xref: /xv6-public/main.c (revision be0a7eac)
1 #include "types.h"
2 #include "param.h"
3 #include "mmu.h"
4 #include "proc.h"
5 #include "defs.h"
6 #include "x86.h"
7 #include "traps.h"
8 #include "syscall.h"
9 
10 extern char edata[], end[];
11 
12 int
13 main()
14 {
15   struct proc *p;
16   int i;
17 
18   // clear BSS
19   memset(edata, 0, end - edata);
20 
21   // partially initizialize PIC
22   outb(0x20+1, 0xFF); // IO_PIC1
23   outb(0xA0+1, 0xFF); // IO_PIC2
24   outb(0x20, 0x11);
25   outb(0x20+1, 32);
26 
27   cprintf("\nxV6\n\n");
28 
29   kinit(); // physical memory allocator
30   tinit(); // traps and interrupts
31 
32   // create fake process zero
33   p = &proc[0];
34   curproc = p;
35   p->state = WAITING;
36   p->sz = PAGE;
37   p->mem = kalloc(p->sz);
38   memset(p->mem, 0, p->sz);
39   p->kstack = kalloc(KSTACKSIZE);
40   p->tf = (struct Trapframe *) (p->kstack + KSTACKSIZE - sizeof(struct Trapframe));
41   memset(p->tf, 0, sizeof(struct Trapframe));
42   p->tf->tf_es = p->tf->tf_ds = p->tf->tf_ss = (SEG_UDATA << 3) | 3;
43   p->tf->tf_cs = (SEG_UCODE << 3) | 3;
44   p->tf->tf_eflags = FL_IF;
45   p->pid = 0;
46   p->ppid = 0;
47   setupsegs(p);
48 
49   p = newproc();
50 
51   i = 0;
52   p->mem[i++] = 0x90; // nop
53   p->mem[i++] = 0xb8; // mov ..., %eax
54   p->mem[i++] = SYS_fork;
55   p->mem[i++] = 0;
56   p->mem[i++] = 0;
57   p->mem[i++] = 0;
58   p->mem[i++] = 0xcd; // int
59   p->mem[i++] = T_SYSCALL;
60   p->mem[i++] = 0xb8; // mov ..., %eax
61   p->mem[i++] = SYS_wait;
62   p->mem[i++] = 0;
63   p->mem[i++] = 0;
64   p->mem[i++] = 0;
65   p->mem[i++] = 0xcd; // int
66   p->mem[i++] = T_SYSCALL;
67   p->mem[i++] = 0xb8; // mov ..., %eax
68   p->mem[i++] = SYS_exit;
69   p->mem[i++] = 0;
70   p->mem[i++] = 0;
71   p->mem[i++] = 0;
72   p->mem[i++] = 0xcd; // int
73   p->mem[i++] = T_SYSCALL;
74   p->tf->tf_eip = 0;
75   p->tf->tf_esp = p->sz;
76 
77   swtch();
78 
79   return 0;
80 }
81