#include "x86.h"
#define SECTSIZE 512
-#define ELFHDR ((struct elfhdr*) 0x10000) // scratch space
void readseg(uint, uint, uint);
void
cmain(void)
{
+ struct elfhdr *elf;
struct proghdr *ph, *eph;
+ void (*entry)(void);
- // read 1st page off disk
- readseg((uint) ELFHDR, SECTSIZE*8, 0);
+ elf = (struct elfhdr*)0x10000; // scratch space
- // is this a valid ELF?
- if(ELFHDR->magic != ELF_MAGIC)
+ // Read 1st page off disk
+ readseg((uint)elf, SECTSIZE*8, 0);
+
+ // Is this an ELF executable?
+ if(elf->magic != ELF_MAGIC)
goto bad;
- // load each program segment (ignores ph flags)
- ph = (struct proghdr*) ((uchar*) ELFHDR + ELFHDR->phoff);
- eph = ph + ELFHDR->phnum;
+ // Load each program segment (ignores ph flags).
+ ph = (struct proghdr*)((uchar*)elf + elf->phoff);
+ eph = ph + elf->phnum;
for(; ph < eph; ph++)
readseg(ph->va, ph->memsz, ph->offset);
- // call the entry point from the ELF header
- // note: does not return!
- ((void(*)(void)) (ELFHDR->entry & 0xFFFFFF))();
+ // Call the entry point from the ELF header.
+ // Does not return!
+ entry = (void(*)(void))(elf->entry & 0xFFFFFF);
+ entry();
bad:
outw(0x8A00, 0x8A00);
void
waitdisk(void)
{
- // wait for disk reaady
+ // Wait for disk ready.
while((inb(0x1F7) & 0xC0) != 0x40)
;
}
void
readsect(void *dst, uint offset)
{
- // wait for disk to be ready
+ // Issue command.
waitdisk();
-
outb(0x1F2, 1); // count = 1
outb(0x1F3, offset);
outb(0x1F4, offset >> 8);
outb(0x1F6, (offset >> 24) | 0xE0);
outb(0x1F7, 0x20); // cmd 0x20 - read sectors
- // wait for disk to be ready
+ // Read data.
waitdisk();
-
- // read a sector
insl(0x1F0, dst, SECTSIZE/4);
}
void
readseg(uint va, uint count, uint offset)
{
- uint end_va;
+ uint eva;
va &= 0xFFFFFF;
- end_va = va + count;
+ eva = va + count;
- // round down to sector boundary
+ // Round down to sector boundary.
va &= ~(SECTSIZE - 1);
- // translate from bytes to sectors, and kernel starts at sector 1
+ // Translate from bytes to sectors; kernel starts at sector 1.
offset = (offset / SECTSIZE) + 1;
// If this is too slow, we could read lots of sectors at a time.
// We'd write more to memory than asked, but it doesn't matter --
// we load in increasing order.
- while(va < end_va) {
- readsect((uchar*) va, offset);
- va += SECTSIZE;
- offset++;
- }
+ for(; va < eva; va += SECTSIZE, offset++)
+ readsect((uchar*)va, offset);
}
-
#include "proc.h"
#include "kbd.h"
-struct spinlock console_lock;
+#define CRTPORT 0x3d4
+#define LPTPORT 0x378
+static ushort *crt = (ushort*)0xb8000; // CGA memory
+
+static struct spinlock console_lock;
int panicked = 0;
int use_console_lock = 0;
{
int i;
- for(i = 0; !(inb(0x378+1) & 0x80) && i < 12800; i++)
+ for(i = 0; !(inb(LPTPORT+1) & 0x80) && i < 12800; i++)
;
- outb(0x378+0, c);
- outb(0x378+2, 0x08|0x04|0x01);
- outb(0x378+2, 0x08);
+ outb(LPTPORT+0, c);
+ outb(LPTPORT+2, 0x08|0x04|0x01);
+ outb(LPTPORT+2, 0x08);
}
static void
cons_putc(int c)
{
- int crtport = 0x3d4; // io port of CGA
- ushort *crt = (ushort*) 0xB8000; // base of CGA memory
int ind;
if(panicked){
lpt_putc(c);
// cursor position, 16 bits, col + 80*row
- outb(crtport, 14);
- ind = inb(crtport + 1) << 8;
- outb(crtport, 15);
- ind |= inb(crtport + 1);
+ outb(CRTPORT, 14);
+ ind = inb(CRTPORT + 1) << 8;
+ outb(CRTPORT, 15);
+ ind |= inb(CRTPORT + 1);
c &= 0xff;
if(c == '\n'){
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);
+ outb(CRTPORT, 14);
+ outb(CRTPORT + 1, ind >> 8);
+ outb(CRTPORT, 15);
+ outb(CRTPORT + 1, ind);
}
void
printint(int xx, int base, int sgn)
{
+ static char digits[] = "0123456789ABCDEF";
char buf[16];
- char digits[] = "0123456789ABCDEF";
int i = 0, neg = 0;
uint x;
return cp->sz - n;
}
-// Set up CPU's segment descriptors and task state for a
-// given process.
-// If p==0, set up for "idle" state for when scheduler()
-// is idling, not running any process.
+// Set up CPU's segment descriptors and task state for a given process.
+// If p==0, set up for "idle" state for when scheduler() is running.
void
setupsegs(struct proc *p)
{
- struct cpu *c = &cpus[cpu()];
-
+ struct cpu *c;
+
+ c = &cpus[cpu()];
c->ts.ss0 = SEG_KDATA << 3;
if(p)
c->ts.esp0 = (uint)(p->kstack + KSTACKSIZE);