18 /* Size of kernel static code and data, in 4 kB pages. */
21 /* Amount of physical memory, in 4 kB pages. */
24 static void init_page_table (void);
25 static void setup_gdt (void);
26 void power_off (void);
31 tfunc (void *aux UNUSED)
36 if (random_ulong () % 5 == 0)
38 printk ("%s exiting\n", thread_current ()->name);
41 count = random_ulong () % 25 * 1000000;
42 printk ("%s waiting %zu: ", thread_current ()->name, count);
43 for (i = 0; i < count; i++);
44 printk ("%s\n", thread_current ()->name);
51 extern char _text, _end, __bss_start;
53 /* Clear out the BSS segment. */
54 memset (&__bss_start, 0, &_end - &__bss_start);
59 /* Calculate how much RAM the kernel uses, and find out from
60 the bootloader how much RAM this machine has. */
61 kernel_pages = (&_end - &_text + 4095) / 4096;
62 ram_pages = *(uint32_t *) (0x7e00 - 8);
64 printk ("Initializing nachos-x86, %d kB RAM detected.\n",
67 /* Memory from the end of the kernel through the end of memory
68 is free. Give it to the page allocator. */
69 palloc_init ((void *) (KERN_BASE + kernel_pages * NBPG),
70 (void *) (PHYS_BASE + ram_pages * NBPG));
86 printk ("running semaphore test... ");
95 for (i = 0; i < 4; i++)
100 t = thread_create (name, tfunc, NULL);
109 /* Populates the page directory and page table with the kernel
112 init_page_table (void)
117 pd = palloc_get (PAL_ASSERT | PAL_ZERO);
119 for (paddr = 0; paddr < NBPG * ram_pages; paddr += NBPG)
121 uint32_t vaddr = paddr + PHYS_BASE;
122 size_t pde_idx = PDENO(vaddr);
123 size_t pte_idx = PTENO(vaddr);
125 if (pd[pde_idx] == 0)
127 pt = palloc_get (PAL_ASSERT | PAL_ZERO);
128 pd[pde_idx] = (uint32_t) vtop (pt) | PG_U | PG_W | PG_P;
131 pt[pte_idx] = paddr | PG_U | PG_W | PG_P;
134 /* Set the page table. */
135 asm volatile ("movl %0,%%cr3" :: "r" (vtop (pd)));
139 make_seg_desc (uint32_t base,
141 enum seg_system system,
144 enum seg_granularity granularity)
146 uint32_t e0 = ((limit & 0xffff) /* Limit 15:0. */
147 | (base << 16)); /* Base 15:0. */
148 uint32_t e1 = (((base >> 16) & 0xff) /* Base 23:16. */
149 | ( system << 12) /* 0=system, 1=code/data. */
150 | ( type << 8) /* Segment type. */
151 | (dpl << 13) /* Descriptor privilege. */
152 | (1 << 15) /* Present. */
153 | (limit & 0xf0000) /* Limit 16:19. */
154 | (1 << 22) /* 32-bit segment. */
155 | ( granularity << 23) /* Byte/page granularity. */
156 | (base & 0xff000000)); /* Base 31:24. */
157 return e0 | ((uint64_t) e1 << 32);
161 make_code_desc (int dpl)
163 return make_seg_desc (0, 0xfffff, SYS_CODE_DATA, TYPE_CODE | TYPE_READABLE,
168 make_data_desc (int dpl)
170 return make_seg_desc (0, 0xfffff, SYS_CODE_DATA, TYPE_WRITABLE,
175 make_tss_desc (uint32_t base)
177 return make_seg_desc (base, 0x67, SYS_SYSTEM, TYPE_TSS_32_A, 0, GRAN_BYTE);
180 uint64_t gdt[SEL_CNT];
184 /* Sets up a proper GDT. The bootstrap loader's GDT didn't
185 include user-mode selectors or a TSS. */
189 uint64_t gdtr_operand;
191 /* Our TSS is never used in a call gate or task gate, so only a
192 few fields of it are ever referenced, and those are the only
193 ones we initialize. */
194 tss = palloc_get (PAL_ASSERT | PAL_ZERO);
195 tss->esp0 = (uint32_t) ptov(0xc0020000);
196 tss->ss0 = SEL_KDSEG;
197 tss->bitmap = 0xdfff;
199 /* Initialize GDT. */
200 gdt[SEL_NULL / sizeof *gdt] = 0;
201 gdt[SEL_KCSEG / sizeof *gdt] = make_code_desc (0);
202 gdt[SEL_KDSEG / sizeof *gdt] = make_data_desc (0);
203 gdt[SEL_UCSEG / sizeof *gdt] = make_code_desc (3);
204 gdt[SEL_UDSEG / sizeof *gdt] = make_data_desc (3);
205 gdt[SEL_TSS / sizeof *gdt] = make_tss_desc (vtop (tss));
208 gdtr_operand = make_dtr_operand (sizeof gdt - 1, gdt);
209 asm volatile ("lgdt %0" :: "m" (gdtr_operand));
210 asm volatile ("ltr %w0" :: "r" (SEL_TSS));
216 const char s[] = "Shutdown";
219 printk ("Powering off...\n");
220 for (p = s; *p != '\0'; p++)