~cypheon/xos

b2547584ec8bf9723cd5e49697130506b5a65ca6 — Johann Rudloff 8 years ago 666e83d
Minor improvements, dont hardcode memory map base address.
6 files changed, 23 insertions(+), 25 deletions(-)

M boot1.asm
M boot2.c
M entry.asm
M include/sys/memory.h
M kernel.c
M kmem.c
M boot1.asm => boot1.asm +1 -4
@@ 107,10 107,7 @@ protected_main:
  mov eax, 0x8000
  mov esp, eax

  call boot2_main
  .idle:
  hlt
  jmp .idle
  jmp boot2_main

; pread_sector(unit32_t lba, uint8_t *dest)
%define DRIVE_PORT_BASE 0x01f0

M boot2.c => boot2.c +1 -1
@@ 78,7 78,7 @@ void boot2_main(void) {
    fs_inode_read_block(&fs, &ino_kernel, i, dst);
    dst += fs.block_size;
  }
  asm volatile ("ljmp $0x08,$0x100000" : : );
  asm volatile ("pushl $0x1000\nljmp $0x08,$0x100000" : : );
}

static void

M entry.asm => entry.asm +6 -4
@@ 4,16 4,14 @@ extern kernel_main
%include "assym.asm"

start:
  mov ebx, [esp]
  mov ax, 0x10
  mov ss, ax
  mov esp, 0x200000 ; this gives us 2MiB - kernel_size of stack

  push ebx
  call kernel_main

  .hang:
  hlt
  jmp .hang

%define USER_CS 0x1b
%define USER_DS 0x23



@@ 21,8 19,12 @@ global proc_switch
proc_switch:
  mov ebp, [esp + 4] ; struct proc*

  mov ebx, cr3
  mov eax, PROC_PD(ebp)
  cmp eax, ebx
  je .have_cr3 ; CR3 is already set, no need to flush TLB
  mov cr3, eax
.have_cr3:

  mov ebp, PROC_PCB(ebp)


M include/sys/memory.h => include/sys/memory.h +3 -1
@@ 5,6 5,8 @@

#define alloca(size) __builtin_alloca(size)

struct memory_map_entry;

void *kmalloc(size_t size);
void *kmalloc_align(size_t alignment, size_t size);
void kfree(void *p);


@@ 18,6 20,6 @@ static inline void *memset(void *b, int c, size_t len) {
  return b;
}

void kmem_init(void);
void kmem_init(uint32_t memory_map_entry_count, struct memory_map_entry *entries);

#endif /* _MEMORY_H_ */

M kernel.c => kernel.c +5 -5
@@ 45,18 45,18 @@ static void setup_tss(void);

static struct proc *launch(const char *binary, uint32_t mem);

void kernel_main() {
void kernel_main(void *meminfo) {
  kprint("kernel main entered\n");
  console_init(&global_console);
  console_clear(&global_console);
  console_print(&global_console, "xos kernel loaded\n");

  setup_gdt();

  kmem_init();
  kmem_init(*(uint32_t *)meminfo, meminfo + 4);
  paging_init();


  console_clear(&global_console);
  console_print(&global_console, "xos kernel loaded\n");

  setup_idt();

  struct proc *proc1 = launch("/init", 0x70000000);

M kmem.c => kmem.c +7 -10
@@ 9,7 9,7 @@
#define KMEM_ARENA_START 0x200000
#define KMEM_ARENA_END 0x400000

struct detect_mmap_info {
struct memory_map_entry {
  uint32_t base_low;
  uint32_t base_high;
  uint32_t length_low;


@@ 28,10 28,10 @@ struct free_info {

static struct kmem kmem_state;

void kmem_detect(void);
void kmem_detect(uint32_t count, struct memory_map_entry *mmap);

void kmem_init(void) {
  kmem_detect();
void kmem_init(uint32_t memory_map_entry_count, struct memory_map_entry *entries) {
  kmem_detect(memory_map_entry_count, entries);

  // begin with the last page
  void *page = (void *)(KMEM_ARENA_END - PAGE_SIZE);


@@ 50,12 50,9 @@ void kmem_init(void) {
}

void
kmem_detect(void) {
  uint32_t mmap_count = *(uint32_t *)0x1000;
  struct detect_mmap_info *mmap = (struct detect_mmap_info *)(0x1000 + 4);
  khexprint("mmap count", mmap_count);
  khexprint("mmap size", sizeof(struct detect_mmap_info));
  for (int i=0; i<mmap_count; ++i) {
kmem_detect(uint32_t count, struct memory_map_entry *mmap) {
  khexprint("mmap count", count);
  for (int i=0; i<count; ++i) {
    khexprint("=========== mmap entry", i);
    khexprint("base_low", mmap[i].base_low);
    khexprint("base_high", mmap[i].base_high);