Loading [MathJax]/extensions/tex2jax.js
CS350 COS
COS
All Data Structures Files Functions Variables Typedefs Macros
mbentry.c File Reference
#include <stdbool.h>
#include <stdint.h>
#include <sys/kassert.h>
#include <sys/cdefs.h>
#include "../dev/console.h"
#include <machine/amd64.h>
#include <machine/pmap.h>
#include <machine/multiboot.h>
Include dependency graph for mbentry.c:

Go to the source code of this file.

Macros

#define CHECK_FLAG(flag, bit)   ((flag) & (1 << (bit)))
 
#define PAGE_ALIGN   __attribute__((aligned(PGSIZE)))
 
#define DATA_SECTION   __attribute__((section(".data")))
 
#define MAX_REGIONS   16
 

Functions

void MachineBoot_Entry (unsigned long magic, unsigned long addr)
 
void Machine_EarlyInit ()
 
void Machine_Init ()
 
void PAlloc_AddRegion (uintptr_t start, uintptr_t len)
 
void MachineBoot_AddMem ()
 

Variables

PAGE_ALIGN DATA_SECTION PageTable bootpgtbl3
 
PAGE_ALIGN DATA_SECTION PageTable bootpgtbl2
 
PAGE_ALIGN DATA_SECTION PageTable bootpgtbl1
 
static uintptr_t memRegionStart [MAX_REGIONS]
 
static uintptr_t memRegionLen [MAX_REGIONS]
 
static int memRegionIdx
 

Macro Definition Documentation

◆ CHECK_FLAG

#define CHECK_FLAG (   flag,
  bit 
)    ((flag) & (1 << (bit)))

Definition at line 19 of file mbentry.c.

◆ DATA_SECTION

#define DATA_SECTION   __attribute__((section(".data")))

Definition at line 22 of file mbentry.c.

◆ MAX_REGIONS

#define MAX_REGIONS   16

Definition at line 74 of file mbentry.c.

◆ PAGE_ALIGN

#define PAGE_ALIGN   __attribute__((aligned(PGSIZE)))

Definition at line 21 of file mbentry.c.

Function Documentation

◆ Machine_EarlyInit()

void Machine_EarlyInit ( )

Machine_EarlyInit –

Initializes early kernel state.

Definition at line 146 of file machine.c.

147{
152 Console_Init();
153 PAlloc_Init();
154}
void Console_Init()
Definition: console.c:22
void Critical_Enter()
Definition: critical.c:28
void Critical_Init()
Definition: critical.c:17
void PAlloc_Init()
void Spinlock_EarlyInit()
void WaitChannel_EarlyInit()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Machine_Init()

void Machine_Init ( )

Machine_Init –

At this point the assembly startup code has setup temporary processor data structures sufficient to execute C code and make it through this initialization routine.

Definition at line 169 of file machine.c.

170{
171 /*
172 * Initialize Processor State
173 */
176 Trap_Init();
178 clts(); // Enable FPU/XMM
179
180 /*
181 * Initialize Memory Allocation and Virtual Memory
182 */
183 PAlloc_AddRegion(DMPA2VA(16*1024*1024), 16*1024*1024);
184 PMap_Init();
185 XMem_Init();
188
189 /*
190 * Initialize Time Keeping
191 */
192 KTime_Init();
193 RTC_Init(); // Finishes initializing KTime
194
195 /*
196 * Initialize Interrupts
197 */
198 IRQ_Init();
199 LAPIC_Init();
200 IOAPIC_Init();
201 IOAPIC_Enable(0); // Enable timer interrupts
202 Thread_Init();
203
204 KTimer_Init(); // Depends on RTC and KTime
205
206 /*
207 * Initialize Additional Processors
208 */
209 MP_Init();
210
211 /*
212 * Initialize Basic Devices
213 */
214 PS2_Init(); // PS2 Keyboard
215 PCI_Init(); // PCI BUS
216 IDE_Init(); // IDE Disk Controller
218
219 /*
220 * Open the primary disk and mount the root file system
221 */
222 Disk *root = Disk_GetByID(0, 0);
223 if (!root)
224 Panic("No boot disk!");
225 VFS_MountRoot(root);
226
228
229 /*
230 * Create the idle thread
231 */
233 if (thr == NULL) {
234 kprintf("Couldn't create idle thread!\n");
235 }
237
238 /*
239 * Load the init processor
240 */
242
243 breakpoint();
244}
void MP_Init()
Definition: mp.c:91
static INLINE void clts()
Definition: amd64op.h:372
static INLINE void breakpoint()
Definition: amd64op.h:29
void BufCache_Init()
void Critical_Exit()
Definition: critical.c:35
Disk * Disk_GetByID(uint64_t ctrlNo, uint64_t diskNo)
Definition: disk.c:31
Thread * Thread_KThreadCreate(void(*f)(void *), void *arg)
Definition: thread.c:136
void Thread_Init()
Definition: thread.c:53
void Sched_SetRunnable(Thread *thr)
Definition: sched.c:77
void IOAPIC_Init()
Definition: ioapic.c:54
void IOAPIC_Enable(int irq)
Definition: ioapic.c:70
void IRQ_Init()
Definition: irq.c:14
int kprintf(const char *fmt,...)
Definition: printf.c:210
void LAPIC_Init()
Definition: lapic.c:179
void PCI_Init()
Definition: pci.c:27
void IDE_Init()
Definition: ide.c:86
static void Machine_SyscallInit()
Definition: machine.c:128
void Loader_LoadInit()
Definition: loader.c:187
static void Machine_GDTInit()
Definition: machine.c:53
static void Machine_IdleThread(void *test)
Definition: machine.c:157
static void Machine_TSSInit()
Definition: machine.c:94
void PS2_Init()
Definition: ps2.c:274
void RTC_Init()
Definition: rtc.c:23
void MachineBoot_AddMem()
Definition: mbentry.c:192
void KTime_Init()
Definition: ktime.c:26
void KTimer_Init()
void PAlloc_LateInit()
Definition: palloc.c:71
void PAlloc_AddRegion(uintptr_t start, uintptr_t len)
Definition: palloc.c:92
void PMap_Init()
Definition: pmap.c:19
#define DMPA2VA(pa)
Definition: pmap.h:48
#define NULL
Definition: stddef.h:6
Definition: disk.h:11
Definition: thread.h:31
void Trap_Init()
Definition: trap.c:33
int VFS_MountRoot(Disk *root)
Definition: vfs.c:36
void Panic(const char *str)
Definition: vgacons.c:164
void XMem_Init()
Definition: xmem.c:28
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MachineBoot_AddMem()

void MachineBoot_AddMem ( )

Definition at line 192 of file mbentry.c.

193{
194 int i;
195 uintptr_t initRamEnd = 32*1024*1024;
196
197 for (i = 0; i < memRegionIdx; i++)
198 {
199 uintptr_t start = memRegionStart[i];
201
202 if (start + len < initRamEnd)
203 continue;
204
205 if (start < initRamEnd) {
206 len = initRamEnd - start;
207 start = initRamEnd;
208 }
209
210 kprintf("AddRegion: %08llx %08llx\n", start, len);
212 }
213}
static uintptr_t memRegionLen[MAX_REGIONS]
Definition: mbentry.c:77
static uintptr_t memRegionStart[MAX_REGIONS]
Definition: mbentry.c:76
static int memRegionIdx
Definition: mbentry.c:78
uint64_t len
Definition: multiboot.h:2
#define MEM_DIRECTMAP_BASE
Definition: pmap.h:40
uint64_t uintptr_t
Definition: types.h:16
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MachineBoot_Entry()

void MachineBoot_Entry ( unsigned long  magic,
unsigned long  addr 
)

Definition at line 81 of file mbentry.c.

82{
84
86
87 /* @r{Am I booted by a Multiboot-compliant boot loader?} */
88 if (magic != MULTIBOOT_BOOTLOADER_MAGIC)
89 {
90 kprintf("Invalid magic number: 0x%x\n", magic);
91 //return;
92 }
93
94 /* @r{Print out the flags.} */
95 kprintf("flags = 0x%x\n", (uint64_t) mbi->flags);
96
97 /* @r{Are mem_* valid?} */
98 if (CHECK_FLAG (mbi->flags, 0))
99 kprintf("mem_lower = %uKB, mem_upper = %uKB\n",
100 (unsigned) mbi->mem_lower, (unsigned) mbi->mem_upper);
101
102 /* @r{Is boot_device valid?} */
103 if (CHECK_FLAG (mbi->flags, 1))
104 kprintf("boot_device = 0x%x\n", (unsigned) mbi->boot_device);
105
106 /* @r{Is the command line passed?} */
107 if (CHECK_FLAG (mbi->flags, 2))
108 kprintf("cmdline = %s\n", (char *)(uintptr_t)mbi->cmdline);
109
110 /* @r{Are mods_* valid?} */
111 if (CHECK_FLAG (mbi->flags, 3))
112 {
114 int i;
115
116 kprintf("mods_count = %d, mods_addr = 0x%x\n",
117 (int) mbi->mods_count, (int) mbi->mods_addr);
118 for (i = 0, mod = (multiboot_module_t *)(uintptr_t)mbi->mods_addr;
119 i < mbi->mods_count;
120 i++, mod++)
121 kprintf(" mod_start = 0x%x, mod_end = 0x%x, cmdline = %s\n",
122 (unsigned) mod->mod_start,
123 (unsigned) mod->mod_end,
124 (char *)(uintptr_t) mod->cmdline);
125 }
126
127 /* @r{Bits 4 and 5 are mutually exclusive!} */
128 if (CHECK_FLAG (mbi->flags, 4) && CHECK_FLAG (mbi->flags, 5))
129 {
130 kprintf("Both bits 4 and 5 are set.\n");
131 return;
132 }
133
134 /* @r{Is the symbol table of a.out valid?} */
135 if (CHECK_FLAG (mbi->flags, 4))
136 {
137 multiboot_aout_symbol_table_t *multiboot_aout_sym = &(mbi->u.aout_sym);
138
139 kprintf("multiboot_aout_symbol_table: tabsize = 0x%0x\n"
140 " strsize = 0x%x, addr = 0x%x\n",
141 (unsigned) multiboot_aout_sym->tabsize,
142 (unsigned) multiboot_aout_sym->strsize,
143 (unsigned) multiboot_aout_sym->addr);
144 }
145
146 /* @r{Is the section header table of ELF valid?} */
147 if (CHECK_FLAG (mbi->flags, 5))
148 {
149 multiboot_elf_section_header_table_t *multiboot_elf_sec = &(mbi->u.elf_sec);
150
151 kprintf("multiboot_elf_sec: num = %u, size = 0x%x,"
152 " addr = 0x%x, shndx = 0x%x\n",
153 (unsigned) multiboot_elf_sec->num, (unsigned) multiboot_elf_sec->size,
154 (unsigned) multiboot_elf_sec->addr, (unsigned) multiboot_elf_sec->shndx);
155 }
156
157 memRegionIdx = 0;
158
159 /* @r{Are mmap_* valid?} */
160 if (CHECK_FLAG (mbi->flags, 6))
161 {
163
164 kprintf("mmap_addr = 0x%x, mmap_length = 0x%x\n",
165 (unsigned) mbi->mmap_addr, (unsigned) mbi->mmap_length);
167 (unsigned long) mmap < mbi->mmap_addr + mbi->mmap_length;
168 mmap = (multiboot_memory_map_t *) ((unsigned long) mmap
169 + mmap->size + sizeof (mmap->size)))
170 {
171 kprintf(" size = 0x%x, base_addr = 0x%llx,"
172 " length = 0x%llx, type = 0x%x\n",
173 (unsigned) mmap->size,
174 mmap->addr,
175 mmap->len,
176 (unsigned) mmap->type);
177 if (mmap->type == MULTIBOOT_MEMORY_AVAILABLE) {
180 memRegionIdx++;
181 }
182 }
183 }
184
185 // Main initialization
186 Machine_Init();
187
188 return;
189}
#define CHECK_FLAG(flag, bit)
Definition: mbentry.c:19
void Machine_Init()
Definition: machine.c:169
void Machine_EarlyInit()
Definition: machine.c:146
void * mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
Definition: mman.c:35
union multiboot_info::@0 u
uint64_t addr
Definition: multiboot.h:1
uint32_t mod_start
Definition: multiboot.h:240
uint32_t boot_device
Definition: multiboot.h:147
uint32_t flags
Definition: multiboot.h:140
uint32_t cmdline
Definition: multiboot.h:244
uint32_t mod_end
Definition: multiboot.h:241
uint32_t mmap_length
Definition: multiboot.h:163
#define MULTIBOOT_MEMORY_AVAILABLE
Definition: multiboot.h:3
uint32_t mem_lower
Definition: multiboot.h:143
uint32_t mem_upper
Definition: multiboot.h:144
uint32_t mods_addr
Definition: multiboot.h:154
uint32_t cmdline
Definition: multiboot.h:150
#define MULTIBOOT_BOOTLOADER_MAGIC
Definition: multiboot.h:33
uint32_t mmap_addr
Definition: multiboot.h:164
uint32_t mods_count
Definition: multiboot.h:153
Definition: multiboot.h:224
unsigned long uint64_t
Definition: types.h:13
Here is the call graph for this function:

◆ PAlloc_AddRegion()

void PAlloc_AddRegion ( uintptr_t  start,
uintptr_t  len 
)

PAlloc_AddRegion –

Add a physical memory region to the page allocator.

Definition at line 92 of file palloc.c.

93{
94 uintptr_t i;
95 FreePage *pg;
96
97 if ((start % PGSIZE) != 0)
98 Panic("Region start is not page aligned!");
99 if ((len % PGSIZE) != 0)
100 Panic("Region length is not page aligned!");
101
102 /*
103 * PageInfo table isn't initialized on the first call to this function. We
104 * must allocate a temporary table that will be copied into the XMem region
105 * inside PAlloc_LateInit.
106 *
107 * Note that the PageInfo table is invalid for regions that are not added
108 * to the free list such as MMIO regions.
109 */
110 if (pageInfoTable == NULL) {
111 // Physical Address Offsets
113 uintptr_t end = base + len;
114
115 pageInfoLength = ROUNDUP(end / PGSIZE * sizeof(PageInfo), PGSIZE);
116 pageInfoTable = (PageInfo *)start;
117
118 start += pageInfoLength;
120
121 for (i = 0; i < (base / PGSIZE); i++) {
122 pageInfoTable[i].refCount = 1;
123 }
124 for (i = (base / PGSIZE); i < (end / PGSIZE); i++) {
125 pageInfoTable[i].refCount = 0;
126 }
127 for (i = 0; i < (pageInfoLength / PGSIZE); i++) {
128 pageInfoTable[i + (base / PGSIZE)].refCount = 1;
129 }
130 } else {
131 /*
132 * Only the first call to AddRegion should occur before the XMem region
133 * is initialized.
134 */
135
137
139 uintptr_t end = base + len;
140
141 uintptr_t newLength = ROUNDUP(end / PGSIZE * sizeof(PageInfo), PGSIZE);
142
143 if (!XMem_Allocate(pageInfoXMem, newLength))
144 Panic("Cannot allocate XMem region!");
145
146 // Initialize new pages
147 for (i = (base / PGSIZE); i < (end / PGSIZE); i++) {
148 pageInfoTable[i].refCount = 0;
149 }
150 }
151
153 for (i = 0; i < len; i += PGSIZE)
154 {
155 pg = (void *)(start + i);
157
158 totalPages++;
159 freePages++;
160
161 LIST_INSERT_HEAD(&freeList, pg, entries);
162 }
164}
#define ASSERT(_x)
Definition: kassert.h:8
#define ROUNDUP(_x, _n)
Definition: malloc.c:32
#define PGSIZE
Definition: malloc.c:21
PageInfo * pageInfoTable
Definition: palloc.c:43
#define FREEPAGE_MAGIC_FREE
Definition: palloc.c:23
uint64_t refCount
Definition: palloc.c:39
uint64_t magic
Definition: palloc.c:33
uint64_t pageInfoLength
Definition: palloc.c:44
uint64_t totalPages
Definition: palloc.c:28
XMem * pageInfoXMem
Definition: palloc.c:42
Spinlock pallocLock
Definition: palloc.c:27
uint64_t freePages
Definition: palloc.c:29
#define DMVA2PA(dmva)
Definition: pmap.h:47
#define LIST_INSERT_HEAD(head, elm, field)
Definition: queue.h:451
static uint16_t base
Definition: sercons.c:37
void Spinlock_Unlock(Spinlock *lock) __UNLOCK_EX(*lock)
Definition: spinlock.c:109
void Spinlock_Lock(Spinlock *lock) __LOCK_EX(*lock)
Definition: spinlock.c:75
bool XMem_Allocate(XMem *xmem, uintptr_t length)
Definition: xmem.c:92
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ bootpgtbl1

Initial value:
= { .entries = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
[256] = ((uint64_t)&bootpgtbl2 - MEM_DIRECTMAP_BASE) + 0x03,
}}
PAGE_ALIGN DATA_SECTION PageTable bootpgtbl2
Definition: mbentry.c:48

Definition at line 53 of file mbentry.c.

◆ bootpgtbl2

Initial value:
= { .entries = {
0
}}
PAGE_ALIGN DATA_SECTION PageTable bootpgtbl3
Definition: mbentry.c:28

Definition at line 48 of file mbentry.c.

◆ bootpgtbl3

Initial value:
= { .entries = {
0x0000000000000183,
0x0000000000200183,
0x0000000000400183,
0x0000000000600183,
0x0000000000800183,
0x0000000000A00183,
0x0000000000C00183,
0x0000000000E00183,
0x0000000001000183,
0x0000000001200183,
0x0000000001400183,
0x0000000001600183,
0x0000000001800183,
0x0000000001A00183,
0x0000000001C00183,
0x0000000001E00183,
0x0000000002000183,
}}

Definition at line 28 of file mbentry.c.

◆ memRegionIdx

int memRegionIdx
static

Definition at line 78 of file mbentry.c.

◆ memRegionLen

uintptr_t memRegionLen[MAX_REGIONS]
static

Definition at line 77 of file mbentry.c.

◆ memRegionStart

uintptr_t memRegionStart[MAX_REGIONS]
static

Definition at line 76 of file mbentry.c.