-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
286 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <asm/e820.h> | ||
#include <asm/bootparam.h> | ||
|
||
#include <os/types.h> | ||
#include <os/kernel.h> | ||
|
||
struct e820map e820; | ||
|
||
static void early_panic(char *msg) { | ||
early_printk(msg); | ||
early_printk("\npanic in e820"); | ||
__asm__("hlt"); | ||
} | ||
|
||
static void e820_print_map(char *who) { | ||
int i; | ||
|
||
for (i = 0; i < e820.nr_map; i++) { | ||
printk(KERN_INFO " %s: %016Lx - %016Lx ", who, | ||
(unsigned long long) e820.map[i].addr, | ||
(unsigned long long) | ||
(e820.map[i].addr + e820.map[i].size)); | ||
switch (e820.map[i].type) { | ||
case E820_RAM: | ||
printk(KERN_CONT "(usable)\n"); | ||
break; | ||
case E820_RESERVED: | ||
printk(KERN_CONT "(reserved)\n"); | ||
break; | ||
case E820_ACPI: | ||
printk(KERN_CONT "(ACPI data)\n"); | ||
break; | ||
case E820_NVS: | ||
printk(KERN_CONT "(ACPI NVS)\n"); | ||
break; | ||
default: | ||
printk(KERN_CONT "type %u\n", e820.map[i].type); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
static int copy_e820_map(struct e820entry *biosmap, int nr_map) { | ||
if (nr_map < 2) { | ||
return -1; | ||
} | ||
|
||
do { | ||
uint64_t start = biosmap->addr; | ||
uint64_t size = biosmap->size; | ||
uint64_t end = start + size; | ||
uint32_t type = biosmap->type; | ||
|
||
/* Overflow in 64 bits? Ignore the memory map. */ | ||
if (start > end) | ||
return -1; | ||
int x = e820.nr_map; | ||
|
||
if (x == E820MAX) { | ||
printk(KERN_ERR "Ooops! Too many entries in the memory map!\n"); | ||
return; | ||
} | ||
|
||
e820.map[x].addr = start; | ||
e820.map[x].size = size; | ||
e820.map[x].type = type; | ||
e820.nr_map++; | ||
} while (biosmap++, --nr_map); | ||
return 0; | ||
} | ||
|
||
char *machine_specific_memory_setup() { | ||
char *who = "BIOS-e820"; | ||
|
||
// e820 数组中,可能存在某些内存段重复,需要将重复的部分分离; | ||
// 而我们暂不处理这种情况,认为 multiboot2 中的内存段不存在重复。 | ||
// 复制 e820 信息 | ||
if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0) { | ||
early_panic("Cannot find a valid memory map"); | ||
} | ||
printk(KERN_INFO "BIOS-provided physical RAM map:\n"); | ||
|
||
e820_print_map(who); | ||
return who; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <asm/bootparam.h> | ||
|
||
#include <os/init.h> | ||
|
||
struct boot_params boot_params; | ||
|
||
void memory_setup() { | ||
machine_specific_memory_setup(); | ||
} | ||
|
||
void setup_arch() { | ||
memory_setup(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef _ASM_X86_BOOTPARAM_H | ||
#define _ASM_X86_BOOTPARAM_H | ||
|
||
#include <asm/e820.h> | ||
|
||
struct boot_params { | ||
__u32 e820_entries; | ||
struct e820entry e820_map[E820MAX]; | ||
}; | ||
|
||
extern struct boot_params boot_params; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#ifndef _ASM_X86_DESC_H | ||
#define _ASM_X86_DESC_H | ||
|
||
#include <os/types.h> | ||
#include <os/string.h> | ||
|
||
#include <asm/segment.h> | ||
|
||
#define PTR_LOW(x) ((unsigned long long)(x) & 0xFFFF) | ||
#define PTR_MIDDLE(x) (((unsigned long long)(x) >> 16) & 0xFFFF) | ||
#define PTR_HIGH(x) ((unsigned long long)(x) >> 32) | ||
|
||
extern gate_desc idt_table[]; | ||
|
||
struct gate_struct64 { | ||
uint16_t offset_low; | ||
uint16_t segment; | ||
unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1; | ||
uint16_t offset_middle; | ||
uint32_t offset_high; | ||
uint32_t zero1; | ||
} __attribute__((packed)); | ||
|
||
typedef struct gate_struct64 gate_desc; | ||
|
||
enum { | ||
GATE_INTERRUPT = 0xE, | ||
GATE_TRAP = 0xF, | ||
GATE_CALL = 0xC, | ||
GATE_TASK = 0x5, | ||
}; | ||
|
||
static inline void pack_gate(gate_desc *gate, unsigned type, unsigned long func, | ||
unsigned dpl, unsigned ist, unsigned seg) { | ||
gate->offset_low = PTR_LOW(func); | ||
gate->segment = __KERNEL_CS; | ||
gate->ist = ist; | ||
gate->p = 1; | ||
gate->dpl = dpl; | ||
gate->zero0 = 0; | ||
gate->zero1 = 0; | ||
gate->type = type; | ||
gate->offset_middle = PTR_MIDDLE(func); | ||
gate->offset_high = PTR_HIGH(func); | ||
} | ||
|
||
static inline void write_idt_entry(gate_desc *idt, int entry, | ||
const gate_desc *gate) { | ||
memcpy(&idt[entry], gate, sizeof(*gate)); | ||
} | ||
|
||
static inline void _set_gate(int gate, unsigned type, void *addr, | ||
unsigned dpl, unsigned ist, unsigned seg) { | ||
gate_desc s; | ||
pack_gate(&s, type, (unsigned long)addr, dpl, ist, seg); | ||
/* | ||
* does not need to be atomic because it is only done once at | ||
* setup time | ||
*/ | ||
write_idt_entry(idt_table, gate, &s); | ||
} | ||
|
||
static inline void set_intr_gate(unsigned int n, void *addr) { | ||
_set_gate(n, GATE_INTERRUPT, addr, 0, 0, __KERNEL_CS); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef _ASM_X86_SETUP_H | ||
#define _ASM_X86_SETUP_H | ||
|
||
char *machine_specific_memory_setup(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#ifndef _OS_INIT_H | ||
#define _OS_INIT_H | ||
|
||
void setup_arch(); | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
|
||
#include <os/init.h> | ||
|
||
void start_kernel() { | ||
|
||
setup_arch(); | ||
|
||
__asm__ __volatile__("hlt"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include <os/kernel.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters