Added ability to set a title for the console
Added seed value to the maze generator Added a title lock to the console Added a vconsole lock that locks on any virtual console change Changed some of the locks used to free up
This commit is contained in:
4
Makefile
4
Makefile
@ -225,8 +225,8 @@ QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
|
||||
then echo "-gdb tcp::$(GDBPORT)"; \
|
||||
else echo "-s -p $(GDBPORT)"; fi)
|
||||
ifndef CPUS
|
||||
CPUS := 2
|
||||
#CPUS := 1 # For debugging
|
||||
#CPUS := 2
|
||||
CPUS := 1 # For debugging
|
||||
endif
|
||||
QEMUOPTS = -drive file=fs.img,index=1,media=disk,format=raw -drive file=xv6.img,index=0,media=disk,format=raw -smp $(CPUS) -m 512 $(QEMUEXTRA)
|
||||
|
||||
|
131
console.c
131
console.c
@ -41,6 +41,7 @@ struct vconsole
|
||||
int inuse;
|
||||
uint titlebgcol;
|
||||
char proctitle[20];
|
||||
int titlelocked;
|
||||
char status[30];
|
||||
};
|
||||
|
||||
@ -49,6 +50,7 @@ struct vconsole *currentconsole = 0;
|
||||
|
||||
#define C(x) ((x) - '@') // Control-x
|
||||
|
||||
struct vconsole* getvalidprocessconsoleptr(void);
|
||||
void clearconsole(ushort *bufferin);
|
||||
void loadscreenbuffer(ushort *bufferin);
|
||||
void savescreenbuffer(ushort *bufferin);
|
||||
@ -64,6 +66,13 @@ static struct
|
||||
int locking;
|
||||
} cons;
|
||||
|
||||
// Lock for all consoles (to lock when doing switching/creating?)
|
||||
static struct
|
||||
{
|
||||
struct spinlock lock;
|
||||
int locking;
|
||||
} vcons;
|
||||
|
||||
// Split the printint so we can just get the char out in draw title
|
||||
// itoa is the function in the c standard library (?) so i reused the name
|
||||
// since it does something similar
|
||||
@ -116,10 +125,13 @@ void cprintf(char *fmt, ...)
|
||||
uint *argp;
|
||||
char *s;
|
||||
|
||||
struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
|
||||
|
||||
locking = cons.locking;
|
||||
if (locking)
|
||||
{
|
||||
acquire(&cons.lock);
|
||||
//acquire(&cons.lock);
|
||||
acquire(&inconsoleptr->lock);
|
||||
}
|
||||
|
||||
if (fmt == 0)
|
||||
@ -172,7 +184,8 @@ void cprintf(char *fmt, ...)
|
||||
|
||||
if (locking)
|
||||
{
|
||||
release(&cons.lock);
|
||||
//release(&cons.lock);
|
||||
release(&inconsoleptr->lock);
|
||||
}
|
||||
}
|
||||
|
||||
@ -286,6 +299,7 @@ void panic(char *s)
|
||||
struct vconsole* getvalidprocessconsoleptr(void)
|
||||
{
|
||||
struct vconsole* inconsoleptr = 0;
|
||||
|
||||
// Check if a process has actually been created otherwise use the base console
|
||||
if (myproc() == 0x0)
|
||||
{
|
||||
@ -310,12 +324,14 @@ struct vconsole* getvalidprocessconsoleptr(void)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return inconsoleptr;
|
||||
}
|
||||
|
||||
void setconsoleproctitle(struct vconsole* consoleptr, char* newtitle)
|
||||
{
|
||||
if (!consoleptr->titlelocked)
|
||||
{
|
||||
acquire(&consoleptr->lock);
|
||||
//Clear the current buffer
|
||||
memset(consoleptr->proctitle, 0, sizeof(consoleptr->proctitle));
|
||||
@ -324,6 +340,7 @@ void setconsoleproctitle(struct vconsole* consoleptr, char* newtitle)
|
||||
//Redraw the title since its been updated now
|
||||
drawtitle();
|
||||
release(&consoleptr->lock);
|
||||
}
|
||||
}
|
||||
|
||||
#define BACKSPACE 0x100
|
||||
@ -337,7 +354,7 @@ void drawtitle(void)
|
||||
struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
|
||||
|
||||
if (inconsoleptr->inuse)
|
||||
{
|
||||
{
|
||||
int pos = 0;
|
||||
char c = ' ';
|
||||
char menutext[] = "(M)enu";
|
||||
@ -370,7 +387,7 @@ void drawtitle(void)
|
||||
c = ' ';
|
||||
}
|
||||
crt[pos] = (c & 0xff) | inconsoleptr->titlebgcol;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -470,10 +487,11 @@ void consputc(int c)
|
||||
|
||||
int consoleget(void)
|
||||
{
|
||||
//struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
|
||||
int c;
|
||||
|
||||
|
||||
acquire(&cons.lock);
|
||||
|
||||
|
||||
while ((c = kbdgetc()) <= 0)
|
||||
{
|
||||
if (c == 0)
|
||||
@ -482,14 +500,19 @@ int consoleget(void)
|
||||
}
|
||||
}
|
||||
|
||||
release(&cons.lock);
|
||||
release(&cons.lock);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
void consoleintr(int (*getc)(void))
|
||||
{
|
||||
int c, doprocdump = 0, doconsoleswitch = 0, doconsolehome = 0;
|
||||
int c;
|
||||
int doprocdump = 0;
|
||||
int doconsoleswitch = 0;
|
||||
int doconsolehome = 0;
|
||||
//struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
|
||||
struct kbdbuffer* consolekbdbuffer = ¤tconsole->keybuffer;
|
||||
|
||||
acquire(&cons.lock);
|
||||
while ((c = getc()) >= 0)
|
||||
@ -501,18 +524,18 @@ void consoleintr(int (*getc)(void))
|
||||
doprocdump = 1;
|
||||
break;
|
||||
case C('U'): // Kill line.
|
||||
while (input->e != input->w &&
|
||||
input->buf[(input->e - 1) % INPUT_BUF] != '\n')
|
||||
while (consolekbdbuffer->e != consolekbdbuffer->w &&
|
||||
consolekbdbuffer->buf[(consolekbdbuffer->e - 1) % INPUT_BUF] != '\n')
|
||||
{
|
||||
input->e--;
|
||||
consolekbdbuffer->e--;
|
||||
consputc(BACKSPACE);
|
||||
}
|
||||
break;
|
||||
case C('H'):
|
||||
case '\x7f': // Backspace
|
||||
if (input->e != input->w)
|
||||
if (consolekbdbuffer->e != consolekbdbuffer->w)
|
||||
{
|
||||
input->e--;
|
||||
consolekbdbuffer->e--;
|
||||
consputc(BACKSPACE);
|
||||
}
|
||||
break;
|
||||
@ -523,15 +546,15 @@ void consoleintr(int (*getc)(void))
|
||||
doconsolehome = 1;
|
||||
break;
|
||||
default:
|
||||
if (c != 0 && input->e - input->r < INPUT_BUF)
|
||||
if (c != 0 && consolekbdbuffer->e - consolekbdbuffer->r < INPUT_BUF)
|
||||
{
|
||||
c = (c == '\r') ? '\n' : c;
|
||||
input->buf[input->e++ % INPUT_BUF] = c;
|
||||
consolekbdbuffer->buf[consolekbdbuffer->e++ % INPUT_BUF] = c;
|
||||
consputc(c);
|
||||
if (c == '\n' || c == C('D') || input->e == input->r + INPUT_BUF)
|
||||
if (c == '\n' || c == C('D') || consolekbdbuffer->e == consolekbdbuffer->r + INPUT_BUF)
|
||||
{
|
||||
input->w = input->e;
|
||||
wakeup(&(input->r));
|
||||
consolekbdbuffer->w = consolekbdbuffer->e;
|
||||
wakeup(&(consolekbdbuffer->r));
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -583,18 +606,19 @@ int consoleread(struct inode *ip, char *dst, int n)
|
||||
|
||||
iunlock(ip);
|
||||
target = n;
|
||||
acquire(&cons.lock);
|
||||
//acquire(&cons.lock);
|
||||
acquire(&inconsoleptr->lock);
|
||||
while (n > 0)
|
||||
{
|
||||
while (consolekbdbuffer->r == consolekbdbuffer->w)
|
||||
{
|
||||
if (myproc()->killed)
|
||||
{
|
||||
release(&cons.lock);
|
||||
release(&inconsoleptr->lock);
|
||||
ilock(ip);
|
||||
return -1;
|
||||
}
|
||||
sleep(&(consolekbdbuffer->r), &cons.lock);
|
||||
sleep(&(consolekbdbuffer->r), &inconsoleptr->lock);
|
||||
}
|
||||
c = consolekbdbuffer->buf[consolekbdbuffer->r++ % INPUT_BUF];
|
||||
if (c == C('D'))
|
||||
@ -614,7 +638,8 @@ int consoleread(struct inode *ip, char *dst, int n)
|
||||
break;
|
||||
}
|
||||
}
|
||||
release(&cons.lock);
|
||||
//release(&cons.lock);
|
||||
release(&inconsoleptr->lock);
|
||||
ilock(ip);
|
||||
|
||||
return target - n;
|
||||
@ -693,24 +718,21 @@ void clearscreen(void)
|
||||
|
||||
if (inconsoleptr->inuse)
|
||||
{
|
||||
loadscreenbuffer(inconsoleptr->screenbuffer);
|
||||
drawtitle();
|
||||
release(&cons.lock);
|
||||
|
||||
loadscreenbuffer(inconsoleptr->screenbuffer);
|
||||
|
||||
outb(CRTPORT, 14);
|
||||
outb(CRTPORT + 1, pos >> 8);
|
||||
outb(CRTPORT, 15);
|
||||
outb(CRTPORT + 1, pos);
|
||||
}
|
||||
else
|
||||
{
|
||||
release(&cons.lock);
|
||||
}
|
||||
drawtitle();
|
||||
release(&cons.lock);
|
||||
}
|
||||
|
||||
void consoleinit(void)
|
||||
{
|
||||
initlock(&cons.lock, "console");
|
||||
initlock(&vcons.lock, "vconglobal");
|
||||
|
||||
for (int i = 0; i < MAXVCONSOLES; i++)
|
||||
{
|
||||
@ -721,7 +743,9 @@ void consoleinit(void)
|
||||
consoles[i].inuse = 0;
|
||||
consoles[i].titlebgcol = 0x4F00;
|
||||
|
||||
//initlock(&consoles[i].lock, "vconsole" + i);
|
||||
char lockname[8];
|
||||
sprintf(lockname, "vconsole%d", i);
|
||||
initlock(&consoles[i].lock, lockname);
|
||||
}
|
||||
|
||||
// Initialise pointer to point to our console input buffer
|
||||
@ -740,22 +764,33 @@ void consoleinit(void)
|
||||
ioapicenable(IRQ_KBD, 0);
|
||||
}
|
||||
|
||||
struct vconsole* newconsole(int bgpreset)
|
||||
struct vconsole* newconsole(char* title, int bgpreset)
|
||||
{
|
||||
struct vconsole* result = 0;
|
||||
|
||||
acquire(&cons.lock);
|
||||
acquire(&vcons.lock);
|
||||
for (int i = 1; i < MAXVCONSOLES; i++)
|
||||
{
|
||||
if (!consoles[i].active)
|
||||
{
|
||||
consoles[i].processowner = myproc();
|
||||
consoles[i].titlebgcol = bgpreset;
|
||||
|
||||
if (strlen(title) > 0)
|
||||
{
|
||||
safestrcpy(consoles[i].proctitle, title, sizeof(consoles[i].proctitle));
|
||||
consoles[i].titlelocked = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
consoles[i].titlelocked = 0;
|
||||
}
|
||||
|
||||
result = &consoles[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
release(&cons.lock);
|
||||
release(&vcons.lock);
|
||||
|
||||
return result;
|
||||
}
|
||||
@ -771,23 +806,19 @@ void releaseallconsoles(void)
|
||||
int switchtoconsole(struct vconsole* consoleptr)
|
||||
{
|
||||
int pos;
|
||||
|
||||
acquire(&consoleptr->lock);
|
||||
acquire(&cons.lock);
|
||||
|
||||
acquire(&vcons.lock);
|
||||
releaseallconsoles();
|
||||
currentconsole = consoleptr;
|
||||
currentconsole->inuse = 1;
|
||||
input = ¤tconsole->keybuffer;
|
||||
//input = ¤tconsole->keybuffer;
|
||||
// ioapicenable(IRQ_KBD, 0);
|
||||
|
||||
loadscreenbuffer(currentconsole->screenbuffer);
|
||||
drawtitle();
|
||||
release(&cons.lock);
|
||||
|
||||
|
||||
if (!currentconsole->active)
|
||||
{
|
||||
clearscreen();
|
||||
drawtitle();
|
||||
clearscreen();
|
||||
cprintf("Welcome to Console: %d\n", currentconsole->consoleindex);
|
||||
currentconsole->active = 1;
|
||||
}
|
||||
@ -801,7 +832,8 @@ int switchtoconsole(struct vconsole* consoleptr)
|
||||
outb(CRTPORT + 1, pos);
|
||||
}
|
||||
|
||||
release(&consoleptr->lock);
|
||||
drawtitle();
|
||||
release(&vcons.lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -810,7 +842,7 @@ int closeconsole(void)
|
||||
struct vconsole* consoleptr = myproc()->consoleptr;
|
||||
|
||||
//cprintf("Console Owner PID: %d\n", consoleptr->processowner->pid);
|
||||
|
||||
acquire(&vcons.lock);
|
||||
if (myproc() == consoleptr->processowner)
|
||||
{
|
||||
clearconsole(consoleptr->screenbuffer);
|
||||
@ -819,21 +851,26 @@ int closeconsole(void)
|
||||
if (consoleptr->inuse)
|
||||
{
|
||||
consoleptr->inuse = 0;
|
||||
release(&vcons.lock);
|
||||
switchtoconsole(&consoles[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
release(&vcons.lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getcurrentconsoleindex(void)
|
||||
{
|
||||
acquire(&vcons.lock);
|
||||
for (int i = 0; i < MAXVCONSOLES; i++)
|
||||
{
|
||||
if (consoles[i].inuse)
|
||||
{
|
||||
release(&vcons.lock);
|
||||
return consoles[i].consoleindex;
|
||||
}
|
||||
}
|
||||
release(&vcons.lock);
|
||||
return -1;
|
||||
}
|
2
defs.h
2
defs.h
@ -24,7 +24,7 @@ int consoleget(void);
|
||||
void panic(char*) __attribute__((noreturn));
|
||||
|
||||
// Function to return a new console ptr for a process to use
|
||||
struct vconsole* newconsole(int);
|
||||
struct vconsole* newconsole(char*, int);
|
||||
// Function to swithc to the provided console using the ptr
|
||||
int switchtoconsole(struct vconsole*);
|
||||
// Function to close and reset the current proc console
|
||||
|
21
maze.c
21
maze.c
@ -18,7 +18,7 @@ uint rngnumber(void)
|
||||
return (z1 ^ z2 ^ z3 ^ z4) / 2;
|
||||
}
|
||||
|
||||
int rngrange(int start, int end)
|
||||
int rngrange(int start, int end, int seed)
|
||||
{
|
||||
if (end < start)
|
||||
{
|
||||
@ -28,27 +28,36 @@ int rngrange(int start, int end)
|
||||
}
|
||||
|
||||
int range = end - start + 1;
|
||||
return rngnumber() % (range) + start;
|
||||
return (rngnumber() + seed) % (range) + start;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int lines = 1000;
|
||||
int seed = getpid();
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "-l") == 0) {
|
||||
if (strcmp(argv[i], "-l") == 0)
|
||||
{
|
||||
lines = atoi(argv[i + 1]);
|
||||
}
|
||||
else if (strcmp(argv[i], "-s") == 0)
|
||||
{
|
||||
seed = atoi(argv[i + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
cls();
|
||||
printf(1, "Start Maze\n");
|
||||
for (int y = 0; y < lines; y++)
|
||||
{
|
||||
for (int x = 0; x < 79; x++)
|
||||
for (int x = 0; x < 80; x++)
|
||||
{
|
||||
int rndnum = rngrange(189, 197);
|
||||
int rndnum = rngrange(188, 197, seed);
|
||||
switch (rndnum)
|
||||
{
|
||||
case 188:
|
||||
rndnum = 32;
|
||||
break;
|
||||
case 189:
|
||||
rndnum = 179;
|
||||
break;
|
||||
@ -59,7 +68,7 @@ int main(int argc, char *argv[]) {
|
||||
printf(1, "%c", rndnum);
|
||||
|
||||
}
|
||||
printf(1, "\n");
|
||||
//printf(1, "\n");
|
||||
sleep(10);
|
||||
}
|
||||
printf(1, "Maze Ended\n");
|
||||
|
96
screen.c
96
screen.c
@ -3,43 +3,21 @@
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int pid;
|
||||
int selopt = getpid() % 10;
|
||||
int bgcol = 0x4F00;
|
||||
char title[20];
|
||||
int accepttitle = 0;
|
||||
int currenttitlelen = 0;
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "-bg") == 0)
|
||||
{
|
||||
int selopt = atoi(argv[i + 1]);
|
||||
switch(selopt)
|
||||
{
|
||||
case 1:
|
||||
bgcol = 0x1F00;
|
||||
break;
|
||||
case 2:
|
||||
bgcol = 0x2F00;
|
||||
break;
|
||||
case 3:
|
||||
bgcol = 0x3F00;
|
||||
break;
|
||||
case 4:
|
||||
bgcol = 0x5F00;
|
||||
break;
|
||||
case 5:
|
||||
bgcol = 0xF000;
|
||||
break;
|
||||
case 6:
|
||||
bgcol = 0x8F00;
|
||||
break;
|
||||
case 7:
|
||||
bgcol = 0x9F00;
|
||||
break;
|
||||
case 8:
|
||||
bgcol = 0xAF00;
|
||||
break;
|
||||
case 9:
|
||||
bgcol = 0xCF00;
|
||||
break;
|
||||
|
||||
}
|
||||
accepttitle = 0;
|
||||
selopt = atoi(argv[i + 1]);
|
||||
}
|
||||
else if (strcmp(argv[i], "-t") == 0)
|
||||
{
|
||||
accepttitle = 1;
|
||||
}
|
||||
else if (strcmp(argv[i], "-help") == 0)
|
||||
{
|
||||
@ -49,6 +27,58 @@ int main(int argc, char *argv[]) {
|
||||
exit();
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (accepttitle)
|
||||
{
|
||||
if (currenttitlelen > 0)
|
||||
{
|
||||
title[currenttitlelen] = ' ';
|
||||
currenttitlelen++;
|
||||
}
|
||||
for (int x = 0; x <= sizeof(argv[i]); x++)
|
||||
{
|
||||
title[currenttitlelen] = argv[i][x];
|
||||
currenttitlelen++;
|
||||
if (currenttitlelen >= 20)
|
||||
{
|
||||
accepttitle = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch(selopt)
|
||||
{
|
||||
case 1:
|
||||
bgcol = 0x1F00;
|
||||
break;
|
||||
case 2:
|
||||
bgcol = 0x2F00;
|
||||
break;
|
||||
case 3:
|
||||
bgcol = 0x3F00;
|
||||
break;
|
||||
case 4:
|
||||
bgcol = 0x5F00;
|
||||
break;
|
||||
case 5:
|
||||
bgcol = 0xF000;
|
||||
break;
|
||||
case 6:
|
||||
bgcol = 0x8F00;
|
||||
break;
|
||||
case 7:
|
||||
bgcol = 0x9F00;
|
||||
break;
|
||||
case 8:
|
||||
bgcol = 0xAF00;
|
||||
break;
|
||||
case 9:
|
||||
bgcol = 0xCF00;
|
||||
break;
|
||||
}
|
||||
|
||||
pid = fork();
|
||||
@ -56,7 +86,7 @@ int main(int argc, char *argv[]) {
|
||||
printf(1, "screen: fork failed\n");
|
||||
}
|
||||
if (pid == 0) {
|
||||
if (screen(bgcol) != 0)
|
||||
if (screen(title, bgcol) != 0)
|
||||
{
|
||||
exec("sh", argv);
|
||||
printf(1, "screen: exec sh failed\n");
|
||||
|
11
sysproc.c
11
sysproc.c
@ -131,12 +131,19 @@ int sys_screen(void)
|
||||
int result = 0;
|
||||
struct vconsole* consoleptr = 0;
|
||||
int bgcol;
|
||||
if (argint(0, &bgcol) < 0)
|
||||
char *title;
|
||||
|
||||
if (argint(1, &bgcol) < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((consoleptr = newconsole(bgcol)) != 0)
|
||||
if (argstr(0, &title) < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((consoleptr = newconsole(title, bgcol)) != 0)
|
||||
{
|
||||
curproc->consoleptr = consoleptr;
|
||||
switchtoconsole(consoleptr);
|
||||
|
Reference in New Issue
Block a user