2006-07-16 15:40:05 +00:00
|
|
|
#include "types.h"
|
|
|
|
#include "x86.h"
|
2006-06-12 15:22:12 +00:00
|
|
|
#include "defs.h"
|
2006-07-12 01:48:35 +00:00
|
|
|
#include "spinlock.h"
|
2006-08-09 16:04:04 +00:00
|
|
|
#include "dev.h"
|
2006-08-10 22:08:14 +00:00
|
|
|
#include "param.h"
|
2006-07-12 01:48:35 +00:00
|
|
|
|
2006-08-10 22:08:14 +00:00
|
|
|
struct spinlock console_lock;
|
2006-07-17 01:25:22 +00:00
|
|
|
int panicked = 0;
|
2006-07-15 12:03:57 +00:00
|
|
|
int use_console_lock = 0;
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2006-08-10 22:08:14 +00:00
|
|
|
// per-cpu copy of output to help panic/lock debugging
|
|
|
|
char obuf[NCPU][1024];
|
|
|
|
uint obufi[NCPU];
|
|
|
|
|
2006-06-22 15:51:57 +00:00
|
|
|
/*
|
|
|
|
* copy console output to parallel port, which you can tell
|
|
|
|
* .bochsrc to copy to the stdout:
|
|
|
|
* parport1: enabled=1, file="/dev/stdout"
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
lpt_putc(int c)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; !(inb(0x378+1) & 0x80) && i < 12800; i++)
|
|
|
|
;
|
|
|
|
outb(0x378+0, c);
|
|
|
|
outb(0x378+2, 0x08|0x04|0x01);
|
|
|
|
outb(0x378+2, 0x08);
|
|
|
|
}
|
|
|
|
|
2006-07-15 12:03:57 +00:00
|
|
|
static void
|
2006-07-17 01:53:43 +00:00
|
|
|
cons_putc(int c)
|
2006-06-12 15:22:12 +00:00
|
|
|
{
|
|
|
|
int crtport = 0x3d4; // io port of CGA
|
2006-07-20 09:07:53 +00:00
|
|
|
ushort *crt = (ushort *) 0xB8000; // base of CGA memory
|
2006-06-12 15:22:12 +00:00
|
|
|
int ind;
|
|
|
|
|
2006-08-10 22:08:14 +00:00
|
|
|
obuf[rcr4()][obufi[rcr4()]++] = c;
|
|
|
|
if(obufi[rcr4()] >= 1024)
|
|
|
|
obufi[rcr4()] = 0;
|
|
|
|
|
2006-07-17 01:25:22 +00:00
|
|
|
if(panicked){
|
2006-07-15 12:03:57 +00:00
|
|
|
cli();
|
2006-07-17 01:25:22 +00:00
|
|
|
for(;;)
|
2006-07-15 12:03:57 +00:00
|
|
|
;
|
|
|
|
}
|
2006-07-12 01:48:35 +00:00
|
|
|
|
2006-06-22 15:51:57 +00:00
|
|
|
lpt_putc(c);
|
|
|
|
|
2006-06-12 15:22:12 +00:00
|
|
|
// cursor position, 16 bits, col + 80*row
|
|
|
|
outb(crtport, 14);
|
|
|
|
ind = inb(crtport + 1) << 8;
|
|
|
|
outb(crtport, 15);
|
|
|
|
ind |= inb(crtport + 1);
|
|
|
|
|
|
|
|
c &= 0xff;
|
|
|
|
|
|
|
|
if(c == '\n'){
|
|
|
|
ind -= (ind % 80);
|
|
|
|
ind += 80;
|
|
|
|
} else {
|
|
|
|
c |= 0x0700; // black on white
|
|
|
|
crt[ind] = c;
|
2006-07-16 01:52:22 +00:00
|
|
|
ind++;
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if((ind / 80) >= 24){
|
|
|
|
// scroll up
|
2006-07-16 01:47:40 +00:00
|
|
|
memmove(crt, crt + 80, sizeof(crt[0]) * (23 * 80));
|
2006-06-12 15:22:12 +00:00
|
|
|
ind -= 80;
|
|
|
|
memset(crt + ind, 0, sizeof(crt[0]) * ((24 * 80) - ind));
|
|
|
|
}
|
|
|
|
|
|
|
|
outb(crtport, 14);
|
|
|
|
outb(crtport + 1, ind >> 8);
|
|
|
|
outb(crtport, 15);
|
|
|
|
outb(crtport + 1, ind);
|
2006-07-15 12:03:57 +00:00
|
|
|
}
|
2006-07-12 01:48:35 +00:00
|
|
|
|
2006-06-12 15:22:12 +00:00
|
|
|
void
|
|
|
|
printint(int xx, int base, int sgn)
|
|
|
|
{
|
|
|
|
char buf[16];
|
|
|
|
char digits[] = "0123456789ABCDEF";
|
|
|
|
int i = 0, neg = 0;
|
2006-07-17 01:52:13 +00:00
|
|
|
uint x;
|
2006-06-12 15:22:12 +00:00
|
|
|
|
|
|
|
if(sgn && xx < 0){
|
|
|
|
neg = 1;
|
|
|
|
x = 0 - xx;
|
|
|
|
} else {
|
|
|
|
x = xx;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
buf[i++] = digits[x % base];
|
|
|
|
} while((x /= base) != 0);
|
|
|
|
if(neg)
|
|
|
|
buf[i++] = '-';
|
|
|
|
|
2006-07-16 01:52:22 +00:00
|
|
|
while(--i >= 0)
|
2006-07-17 01:53:43 +00:00
|
|
|
cons_putc(buf[i]);
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-07-16 16:00:03 +00:00
|
|
|
* print to the console. only understands %d, %x, %p, %s.
|
2006-06-12 15:22:12 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
cprintf(char *fmt, ...)
|
|
|
|
{
|
2006-08-10 22:08:14 +00:00
|
|
|
int i, state = 0, c, locking = 0;
|
2006-07-17 01:52:13 +00:00
|
|
|
uint *ap = (uint *)(void*)&fmt + 1;
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2006-08-10 22:08:14 +00:00
|
|
|
if(use_console_lock){
|
|
|
|
locking = 1;
|
2006-07-15 12:03:57 +00:00
|
|
|
acquire(&console_lock);
|
2006-08-10 22:08:14 +00:00
|
|
|
}
|
2006-07-15 12:03:57 +00:00
|
|
|
|
2006-06-12 15:22:12 +00:00
|
|
|
for(i = 0; fmt[i]; i++){
|
|
|
|
c = fmt[i] & 0xff;
|
|
|
|
if(state == 0){
|
|
|
|
if(c == '%'){
|
|
|
|
state = '%';
|
|
|
|
} else {
|
2006-07-17 01:53:43 +00:00
|
|
|
cons_putc(c);
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
} else if(state == '%'){
|
|
|
|
if(c == 'd'){
|
|
|
|
printint(*ap, 10, 1);
|
|
|
|
ap++;
|
Changes to allow use of native x86 ELF compilers, which on my
Linux 2.4 box using gcc 3.4.6 don't seem to follow the same
conventions as the i386-jos-elf-gcc compilers.
Can run make 'TOOLPREFIX=' or edit the Makefile.
curproc[cpu()] can now be NULL, indicating that no proc is running.
This seemed safer to me than having curproc[0] and curproc[1]
both pointing at proc[0] potentially.
The old implementation of swtch depended on the stack frame layout
used inside swtch being okay to return from on the other stack
(exactly the V6 you are not expected to understand this).
It also could be called in two contexts: at boot time, to schedule
the very first process, and later, on behalf of a process, to sleep
or schedule some other process.
I split this into two functions: scheduler and swtch.
The scheduler is now a separate never-returning function, invoked
by each cpu once set up. The scheduler looks like:
scheduler() {
setjmp(cpu.context);
pick proc to schedule
blah blah blah
longjmp(proc.context)
}
The new swtch is intended to be called only when curproc[cpu()] is not NULL,
that is, only on behalf of a user proc. It does:
swtch() {
if(setjmp(proc.context) == 0)
longjmp(cpu.context)
}
to save the current proc context and then jump over to the scheduler,
running on the cpu stack.
Similarly the system call stubs are now in assembly in usys.S to avoid
needing to know the details of stack frame layout used by the compiler.
Also various changes in the debugging prints.
2006-07-11 01:07:40 +00:00
|
|
|
} else if(c == 'x' || c == 'p'){
|
2006-06-12 15:22:12 +00:00
|
|
|
printint(*ap, 16, 0);
|
|
|
|
ap++;
|
2006-07-16 16:00:03 +00:00
|
|
|
} else if(c == 's'){
|
|
|
|
char *s = (char*)*ap;
|
|
|
|
ap++;
|
|
|
|
while(*s != 0){
|
2006-07-17 01:53:43 +00:00
|
|
|
cons_putc(*s);
|
2006-07-16 16:00:03 +00:00
|
|
|
s++;
|
|
|
|
}
|
2006-06-12 15:22:12 +00:00
|
|
|
} else if(c == '%'){
|
2006-07-17 01:53:43 +00:00
|
|
|
cons_putc(c);
|
2006-07-16 16:00:03 +00:00
|
|
|
} else {
|
|
|
|
// Unknown % sequence. Print it to draw attention.
|
2006-07-17 01:53:43 +00:00
|
|
|
cons_putc('%');
|
|
|
|
cons_putc(c);
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
}
|
2006-07-15 12:03:57 +00:00
|
|
|
|
2006-08-10 22:08:14 +00:00
|
|
|
if(locking)
|
2006-07-15 12:03:57 +00:00
|
|
|
release(&console_lock);
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
panic(char *s)
|
|
|
|
{
|
2006-07-15 12:03:57 +00:00
|
|
|
__asm __volatile("cli");
|
|
|
|
use_console_lock = 0;
|
2006-08-15 15:53:46 +00:00
|
|
|
cprintf("panic (%d): ", cpu());
|
2006-06-12 15:22:12 +00:00
|
|
|
cprintf(s, 0);
|
|
|
|
cprintf("\n", 0);
|
2006-07-17 01:25:22 +00:00
|
|
|
panicked = 1; // freeze other CPU
|
|
|
|
for(;;)
|
2006-06-12 15:22:12 +00:00
|
|
|
;
|
|
|
|
}
|
2006-08-09 16:04:04 +00:00
|
|
|
|
|
|
|
int
|
2006-08-11 13:55:18 +00:00
|
|
|
console_write (int minor, char *buf, int n)
|
2006-08-09 16:04:04 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2006-08-10 01:28:57 +00:00
|
|
|
acquire(&console_lock);
|
|
|
|
|
2006-08-09 16:04:04 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2006-08-11 13:55:18 +00:00
|
|
|
cons_putc(buf[i] & 0xff);
|
2006-08-09 16:04:04 +00:00
|
|
|
}
|
|
|
|
|
2006-08-10 01:28:57 +00:00
|
|
|
release(&console_lock);
|
|
|
|
|
2006-08-09 16:04:04 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2006-08-10 02:07:10 +00:00
|
|
|
|
|
|
|
/* This is i8042reg.h + kbdreg.h from NetBSD. */
|
|
|
|
#define KBSTATP 0x64 /* kbd controller status port(I) */
|
|
|
|
#define KBS_DIB 0x01 /* kbd data in buffer */
|
|
|
|
#define KBDATAP 0x60 /* kbd data port(I) */
|
|
|
|
|
|
|
|
#define NO 0
|
|
|
|
|
|
|
|
#define SHIFT (1<<0)
|
|
|
|
#define CTL (1<<1)
|
|
|
|
#define ALT (1<<2)
|
|
|
|
|
|
|
|
#define CAPSLOCK (1<<3)
|
|
|
|
#define NUMLOCK (1<<4)
|
|
|
|
#define SCROLLLOCK (1<<5)
|
|
|
|
|
|
|
|
#define E0ESC (1<<6)
|
|
|
|
|
|
|
|
// Special keycodes
|
|
|
|
#define KEY_HOME 0xE0
|
|
|
|
#define KEY_END 0xE1
|
|
|
|
#define KEY_UP 0xE2
|
|
|
|
#define KEY_DN 0xE3
|
|
|
|
#define KEY_LF 0xE4
|
|
|
|
#define KEY_RT 0xE5
|
|
|
|
#define KEY_PGUP 0xE6
|
|
|
|
#define KEY_PGDN 0xE7
|
|
|
|
#define KEY_INS 0xE8
|
|
|
|
#define KEY_DEL 0xE9
|
|
|
|
|
|
|
|
static uchar shiftcode[256] =
|
|
|
|
{
|
|
|
|
[0x1D] CTL,
|
|
|
|
[0x2A] SHIFT,
|
|
|
|
[0x36] SHIFT,
|
|
|
|
[0x38] ALT,
|
|
|
|
[0x9D] CTL,
|
|
|
|
[0xB8] ALT
|
|
|
|
};
|
|
|
|
|
|
|
|
static uchar togglecode[256] =
|
|
|
|
{
|
|
|
|
[0x3A] CAPSLOCK,
|
|
|
|
[0x45] NUMLOCK,
|
|
|
|
[0x46] SCROLLLOCK
|
|
|
|
};
|
|
|
|
|
|
|
|
static uchar normalmap[256] =
|
|
|
|
{
|
|
|
|
NO, 0x1B, '1', '2', '3', '4', '5', '6', // 0x00
|
|
|
|
'7', '8', '9', '0', '-', '=', '\b', '\t',
|
|
|
|
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', // 0x10
|
|
|
|
'o', 'p', '[', ']', '\n', NO, 'a', 's',
|
|
|
|
'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', // 0x20
|
|
|
|
'\'', '`', NO, '\\', 'z', 'x', 'c', 'v',
|
|
|
|
'b', 'n', 'm', ',', '.', '/', NO, '*', // 0x30
|
|
|
|
NO, ' ', NO, NO, NO, NO, NO, NO,
|
|
|
|
NO, NO, NO, NO, NO, NO, NO, '7', // 0x40
|
|
|
|
'8', '9', '-', '4', '5', '6', '+', '1',
|
|
|
|
'2', '3', '0', '.', NO, NO, NO, NO, // 0x50
|
|
|
|
[0x97] KEY_HOME, [0x9C] '\n' /*KP_Enter*/,
|
|
|
|
[0xB5] '/' /*KP_Div*/, [0xC8] KEY_UP,
|
|
|
|
[0xC9] KEY_PGUP, [0xCB] KEY_LF,
|
|
|
|
[0xCD] KEY_RT, [0xCF] KEY_END,
|
|
|
|
[0xD0] KEY_DN, [0xD1] KEY_PGDN,
|
|
|
|
[0xD2] KEY_INS, [0xD3] KEY_DEL
|
|
|
|
};
|
|
|
|
|
|
|
|
static uchar shiftmap[256] =
|
|
|
|
{
|
|
|
|
NO, 033, '!', '@', '#', '$', '%', '^', // 0x00
|
|
|
|
'&', '*', '(', ')', '_', '+', '\b', '\t',
|
|
|
|
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', // 0x10
|
|
|
|
'O', 'P', '{', '}', '\n', NO, 'A', 'S',
|
|
|
|
'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', // 0x20
|
|
|
|
'"', '~', NO, '|', 'Z', 'X', 'C', 'V',
|
|
|
|
'B', 'N', 'M', '<', '>', '?', NO, '*', // 0x30
|
|
|
|
NO, ' ', NO, NO, NO, NO, NO, NO,
|
|
|
|
NO, NO, NO, NO, NO, NO, NO, '7', // 0x40
|
|
|
|
'8', '9', '-', '4', '5', '6', '+', '1',
|
|
|
|
'2', '3', '0', '.', NO, NO, NO, NO, // 0x50
|
|
|
|
[0x97] KEY_HOME, [0x9C] '\n' /*KP_Enter*/,
|
|
|
|
[0xB5] '/' /*KP_Div*/, [0xC8] KEY_UP,
|
|
|
|
[0xC9] KEY_PGUP, [0xCB] KEY_LF,
|
|
|
|
[0xCD] KEY_RT, [0xCF] KEY_END,
|
|
|
|
[0xD0] KEY_DN, [0xD1] KEY_PGDN,
|
|
|
|
[0xD2] KEY_INS, [0xD3] KEY_DEL
|
|
|
|
};
|
|
|
|
|
|
|
|
#define C(x) (x - '@')
|
|
|
|
|
|
|
|
static uchar ctlmap[256] =
|
|
|
|
{
|
|
|
|
NO, NO, NO, NO, NO, NO, NO, NO,
|
|
|
|
NO, NO, NO, NO, NO, NO, NO, NO,
|
|
|
|
C('Q'), C('W'), C('E'), C('R'), C('T'), C('Y'), C('U'), C('I'),
|
|
|
|
C('O'), C('P'), NO, NO, '\r', NO, C('A'), C('S'),
|
|
|
|
C('D'), C('F'), C('G'), C('H'), C('J'), C('K'), C('L'), NO,
|
|
|
|
NO, NO, NO, C('\\'), C('Z'), C('X'), C('C'), C('V'),
|
|
|
|
C('B'), C('N'), C('M'), NO, NO, C('/'), NO, NO,
|
|
|
|
[0x97] KEY_HOME,
|
|
|
|
[0xB5] C('/'), [0xC8] KEY_UP,
|
|
|
|
[0xC9] KEY_PGUP, [0xCB] KEY_LF,
|
|
|
|
[0xCD] KEY_RT, [0xCF] KEY_END,
|
|
|
|
[0xD0] KEY_DN, [0xD1] KEY_PGDN,
|
|
|
|
[0xD2] KEY_INS, [0xD3] KEY_DEL
|
|
|
|
};
|
|
|
|
|
|
|
|
static uchar *charcode[4] = {
|
|
|
|
normalmap,
|
|
|
|
shiftmap,
|
|
|
|
ctlmap,
|
|
|
|
ctlmap
|
|
|
|
};
|
|
|
|
|
|
|
|
#define KBD_BUF 64
|
|
|
|
char kbd_buf[KBD_BUF];
|
|
|
|
int kbd_r;
|
|
|
|
int kbd_w;
|
2006-08-10 22:08:14 +00:00
|
|
|
struct spinlock kbd_lock;
|
2006-08-23 01:09:24 +00:00
|
|
|
static uint shift;
|
2006-08-10 02:07:10 +00:00
|
|
|
|
2006-08-09 16:04:04 +00:00
|
|
|
void
|
2006-08-10 02:07:10 +00:00
|
|
|
kbd_intr()
|
|
|
|
{
|
|
|
|
uint st, data, c;
|
2006-08-23 01:09:24 +00:00
|
|
|
|
|
|
|
acquire(&kbd_lock);
|
2006-08-10 02:07:10 +00:00
|
|
|
|
|
|
|
st = inb(KBSTATP);
|
|
|
|
if ((st & KBS_DIB) == 0){
|
2006-08-23 01:09:24 +00:00
|
|
|
release(&kbd_lock);
|
2006-08-10 02:07:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
data = inb(KBDATAP);
|
|
|
|
|
|
|
|
if (data == 0xE0) {
|
|
|
|
shift |= E0ESC;
|
2006-08-23 01:09:24 +00:00
|
|
|
release(&kbd_lock);
|
2006-08-10 02:07:10 +00:00
|
|
|
return;
|
|
|
|
} else if (data & 0x80) {
|
|
|
|
// Key released
|
|
|
|
data = (shift & E0ESC ? data : data & 0x7F);
|
|
|
|
shift &= ~(shiftcode[data] | E0ESC);
|
2006-08-23 01:09:24 +00:00
|
|
|
release(&kbd_lock);
|
2006-08-10 02:07:10 +00:00
|
|
|
return;
|
|
|
|
} else if (shift & E0ESC) {
|
|
|
|
// Last character was an E0 escape; or with 0x80
|
|
|
|
data |= 0x80;
|
|
|
|
shift &= ~E0ESC;
|
|
|
|
}
|
|
|
|
|
|
|
|
shift |= shiftcode[data];
|
|
|
|
shift ^= togglecode[data];
|
|
|
|
|
|
|
|
c = charcode[shift & (CTL | SHIFT)][data];
|
|
|
|
if (shift & CAPSLOCK) {
|
|
|
|
if ('a' <= c && c <= 'z')
|
|
|
|
c += 'A' - 'a';
|
|
|
|
else if ('A' <= c && c <= 'Z')
|
|
|
|
c += 'a' - 'A';
|
|
|
|
}
|
2006-08-23 01:09:24 +00:00
|
|
|
|
|
|
|
// xxx hack
|
|
|
|
if (c == 0x0) {
|
|
|
|
release(&kbd_lock);
|
|
|
|
return;
|
|
|
|
}
|
2006-08-10 02:07:10 +00:00
|
|
|
|
|
|
|
if(((kbd_w + 1) % KBD_BUF) != kbd_r){
|
|
|
|
kbd_buf[kbd_w++] = c;
|
|
|
|
if(kbd_w >= KBD_BUF)
|
|
|
|
kbd_w = 0;
|
2006-08-11 13:55:18 +00:00
|
|
|
wakeup(&kbd_r);
|
2006-08-10 02:07:10 +00:00
|
|
|
} else {
|
|
|
|
cprintf("kbd overflow\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
release(&kbd_lock);
|
|
|
|
}
|
|
|
|
|
2006-08-11 13:55:18 +00:00
|
|
|
int
|
|
|
|
console_read(int minor, char *dst, int n)
|
|
|
|
{
|
|
|
|
uint target = n;
|
|
|
|
|
|
|
|
acquire(&kbd_lock);
|
|
|
|
|
|
|
|
while(kbd_w == kbd_r)
|
|
|
|
sleep(&kbd_r, &kbd_lock);
|
|
|
|
|
|
|
|
while(n > 0 && kbd_w != kbd_r){
|
2006-08-23 01:09:24 +00:00
|
|
|
*dst = (kbd_buf[kbd_r]) & 0xff;
|
2006-08-11 13:55:18 +00:00
|
|
|
cons_putc(*dst & 0xff);
|
|
|
|
dst++;
|
|
|
|
--n;
|
|
|
|
kbd_r++;
|
|
|
|
if(kbd_r >= KBD_BUF)
|
|
|
|
kbd_r = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
release(&kbd_lock);
|
|
|
|
|
|
|
|
return target - n;
|
|
|
|
}
|
|
|
|
|
2006-08-10 02:07:10 +00:00
|
|
|
void
|
|
|
|
console_init()
|
2006-08-09 16:04:04 +00:00
|
|
|
{
|
2006-08-10 22:08:14 +00:00
|
|
|
initlock(&console_lock, "console");
|
|
|
|
initlock(&kbd_lock, "kbd");
|
|
|
|
|
2006-08-09 16:04:04 +00:00
|
|
|
devsw[CONSOLE].d_write = console_write;
|
2006-08-11 13:55:18 +00:00
|
|
|
devsw[CONSOLE].d_read = console_read;
|
2006-08-10 02:07:10 +00:00
|
|
|
|
|
|
|
ioapic_enable (IRQ_KBD, 1);
|
2006-08-10 22:08:14 +00:00
|
|
|
|
|
|
|
use_console_lock = 1;
|
2006-08-09 16:04:04 +00:00
|
|
|
}
|