25 /* Size of kernel static code and data, in 4 kB pages. */
28 /* Amount of physical memory, in 4 kB pages. */
31 static void ram_init (void);
32 static void gdt_init (void);
33 static void argv_init (void);
34 void power_off (void);
37 main_thread (void *aux UNUSED)
51 /* Initialize components needed by printk() very early. */
55 printk ("Booting cnachos86...\n");
57 /* Calculate how much RAM the kernel uses, and find out from
58 the bootloader how much RAM this machine has. */
59 printk ("ram: detected %'d kB main memory.\n", ram_pages * 4);
61 /* Memory from the end of the kernel through the end of memory
62 is free. Give it to the page allocator. */
63 palloc_init (ptov (LOADER_KERN_BASE + kernel_pages * PGSIZE),
64 ptov (ram_pages * PGSIZE));
79 t = thread_create ("main", main_thread, NULL);
87 make_seg_desc (uint32_t base,
89 enum seg_system system,
92 enum seg_granularity granularity)
94 uint32_t e0 = ((limit & 0xffff) /* Limit 15:0. */
95 | (base << 16)); /* Base 15:0. */
96 uint32_t e1 = (((base >> 16) & 0xff) /* Base 23:16. */
97 | (system << 12) /* 0=system, 1=code/data. */
98 | (type << 8) /* Segment type. */
99 | (dpl << 13) /* Descriptor privilege. */
100 | (1 << 15) /* Present. */
101 | (limit & 0xf0000) /* Limit 16:19. */
102 | (1 << 22) /* 32-bit segment. */
103 | (granularity << 23) /* Byte/page granularity. */
104 | (base & 0xff000000)); /* Base 31:24. */
105 return e0 | ((uint64_t) e1 << 32);
109 make_code_desc (int dpl)
111 return make_seg_desc (0, 0xfffff, SYS_CODE_DATA, TYPE_CODE | TYPE_READABLE,
116 make_data_desc (int dpl)
118 return make_seg_desc (0, 0xfffff, SYS_CODE_DATA, TYPE_WRITABLE,
123 make_tss_desc (void *vaddr)
125 return make_seg_desc ((uint32_t) vaddr,
126 0x67, SYS_SYSTEM, TYPE_TSS_32_A, 0, GRAN_BYTE);
129 uint64_t gdt[SEL_CNT];
133 /* Sets up a proper GDT. The bootstrap loader's GDT didn't
134 include user-mode selectors or a TSS. */
138 uint64_t gdtr_operand;
140 /* Our TSS is never used in a call gate or task gate, so only a
141 few fields of it are ever referenced, and those are the only
142 ones we initialize. */
143 tss = palloc_get (PAL_ASSERT | PAL_ZERO);
144 tss->esp0 = (uint32_t) ptov(0x20000);
145 tss->ss0 = SEL_KDSEG;
146 tss->bitmap = 0xdfff;
148 /* Initialize GDT. */
149 gdt[SEL_NULL / sizeof *gdt] = 0;
150 gdt[SEL_KCSEG / sizeof *gdt] = make_code_desc (0);
151 gdt[SEL_KDSEG / sizeof *gdt] = make_data_desc (0);
152 gdt[SEL_UCSEG / sizeof *gdt] = make_code_desc (3);
153 gdt[SEL_UDSEG / sizeof *gdt] = make_data_desc (3);
154 gdt[SEL_TSS / sizeof *gdt] = make_tss_desc (tss);
157 gdtr_operand = make_dtr_operand (sizeof gdt - 1, gdt);
158 asm volatile ("lgdt %0" :: "m" (gdtr_operand));
159 asm volatile ("ltr %w0" :: "r" (SEL_TSS));
165 /* Start and end of kernel image,
166 and start and end of BSS segment.
167 These are created by kernel.lds. */
168 extern char _start, _end;
169 extern char _start_bss, _end_bss;
171 /* The "BSS" is a segment that should be initialized to zeros.
172 It isn't actually stored on disk or zeroed by the kernel
173 loader, so we have to zero it ourselves. */
174 memset (&_start_bss, 0, &_end_bss - &_start_bss);
176 /* Calculate how much RAM the kernel uses,
177 and find out from the bootloader how much RAM this machine
179 kernel_pages = (&_end - &_start + 4095) / 4096;
180 ram_pages = *(uint32_t *) ptov (LOADER_BASE + LOADER_RAM_PAGES);
186 char *cmd_line = ptov (LOADER_BASE + LOADER_CMD_LINE);
192 const char s[] = "Shutdown";
195 printk ("Powering off...\n");
196 for (p = s; *p != '\0'; p++)