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"
|
|
|
|
|
|
|
|
struct spinlock console_lock;
|
2006-07-15 12:03:57 +00:00
|
|
|
int paniced = 0;
|
|
|
|
int use_console_lock = 0;
|
2006-06-12 15:22:12 +00:00
|
|
|
|
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
|
|
|
|
real_cons_putc(int c)
|
2006-06-12 15:22:12 +00:00
|
|
|
{
|
|
|
|
int crtport = 0x3d4; // io port of CGA
|
|
|
|
unsigned short *crt = (unsigned short *) 0xB8000; // base of CGA memory
|
|
|
|
int ind;
|
|
|
|
|
2006-07-15 12:03:57 +00:00
|
|
|
if(paniced){
|
|
|
|
cli();
|
|
|
|
while(1)
|
|
|
|
;
|
|
|
|
}
|
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-07-15 12:03:57 +00:00
|
|
|
void
|
|
|
|
cons_putc(int c)
|
|
|
|
{
|
|
|
|
if(use_console_lock)
|
|
|
|
acquire(&console_lock);
|
|
|
|
real_cons_putc(c);
|
|
|
|
if(use_console_lock)
|
2006-07-12 11:15:38 +00:00
|
|
|
release(&console_lock);
|
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;
|
|
|
|
unsigned int x;
|
|
|
|
|
|
|
|
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-15 12:03:57 +00:00
|
|
|
real_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, ...)
|
|
|
|
{
|
|
|
|
int i, state = 0, c;
|
2006-07-16 01:15:28 +00:00
|
|
|
unsigned int *ap = (unsigned int *)(void*)&fmt + 1;
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2006-07-15 12:03:57 +00:00
|
|
|
if(use_console_lock)
|
|
|
|
acquire(&console_lock);
|
|
|
|
|
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-15 12:03:57 +00:00
|
|
|
real_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){
|
|
|
|
real_cons_putc(*s);
|
|
|
|
s++;
|
|
|
|
}
|
2006-06-12 15:22:12 +00:00
|
|
|
} else if(c == '%'){
|
2006-07-15 12:03:57 +00:00
|
|
|
real_cons_putc(c);
|
2006-07-16 16:00:03 +00:00
|
|
|
} else {
|
|
|
|
// Unknown % sequence. Print it to draw attention.
|
|
|
|
real_cons_putc('%');
|
|
|
|
real_cons_putc(c);
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
}
|
2006-07-15 12:03:57 +00:00
|
|
|
|
|
|
|
if(use_console_lock)
|
|
|
|
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-07-12 11:15:38 +00:00
|
|
|
cprintf("panic: ");
|
2006-06-12 15:22:12 +00:00
|
|
|
cprintf(s, 0);
|
|
|
|
cprintf("\n", 0);
|
2006-07-15 12:03:57 +00:00
|
|
|
paniced = 1; // freeze other CPU
|
2006-06-12 15:22:12 +00:00
|
|
|
while(1)
|
|
|
|
;
|
|
|
|
}
|