],
"compilerArgs": [
"-ffreestanding",
- "-D__ARCH_IA32"
+ "-D__ARCH_IA32",
+ "-D__LUNAIXOS_DEBUG__"
],
"defines": [],
"compilerPath": "${HOME}/opt/cross-compiler/bin/i686-elf-gcc",
+default=0
+timeout=0
+
menuentry "$_OS_NAME" {
multiboot /boot/$_OS_NAME.bin
}
\ No newline at end of file
export _OS_NAME=$1
-echo $(cat GRUB_TEMPLATE | envsubst)
\ No newline at end of file
+cat GRUB_TEMPLATE | envsubst > "$2"
\ No newline at end of file
#ifndef __LUNAIX_INTERRUPTS_H
#define __LUNAIX_INTERRUPTS_H
+#define FAULT_DIVISION_ERROR 0x0
+#define FAULT_TRAP_DEBUG_EXCEPTION 0x1
+#define INT_NMI 0x2
+#define TRAP_BREAKPOINT 0x3
+#define TRAP_OVERFLOW 0x4
+#define FAULT_BOUND_EXCEED 0x5
+#define FAULT_INVALID_OPCODE 0x6
+#define FAULT_NO_MATH_PROCESSOR 0x7
+#define ABORT_DOUBLE_FAULT 0x8
+#define FAULT_RESERVED_0 0x9
+#define FAULT_INVALID_TSS 0xa
+#define FAULT_SEG_NOT_PRESENT 0xb
+#define FAULT_STACK_SEG_FAULT 0xc
+#define FAULT_GENERAL_PROTECTION 0xd
+#define FAULT_PAGE_FAULT 0xe
+#define FAULT_RESERVED_1 0xf
+#define FAULT_X87_FAULT 0x10
+#define FAULT_ALIGNMENT_CHECK 0x11
+#define ABORT_MACHINE_CHECK 0x12
+#define FAULT_SIMD_FP_EXCEPTION 0x13
+#define FAULT_VIRTUALIZATION_EXCEPTION 0x14
+#define FAULT_CONTROL_PROTECTION 0x15
+
typedef struct {
unsigned int vector;
unsigned int err_code;
void
_asm_isr0();
+void
+_asm_isr13();
+
void
interrupt_handler(isr_param* param);
+++ /dev/null
-// Ref: Intel Manuel Vol.3 Figure 6-1
-
-#ifndef __LUNAIX_TYPES_H
-#define __LUNAIX_TYPES_H
-
-#define FAULT_DIVISION_ERROR 0x0
-#define FAULT_TRAP_DEBUG_EXCEPTION 0x1
-#define INT_NMI 0x2
-#define TRAP_BREAKPOINT 0x3
-#define TRAP_OVERFLOW 0x4
-#define FAULT_BOUND_EXCEED 0x5
-#define FAULT_INVALID_OPCODE 0x6
-#define FAULT_NO_MATH_PROCESSOR 0x7
-#define ABORT_DOUBLE_FAULT 0x8
-#define FAULT_RESERVED_0 0x9
-#define FAULT_INVALID_TSS 0xa
-#define FAULT_SEG_NOT_PRESENT 0xb
-#define FAULT_STACK_SEG_FAULT 0xc
-#define FAULT_GENERAL_PROTECTION 0xd
-#define FAULT_PAGE_FAULT 0xe
-#define FAULT_RESERVED_1 0xf
-#define FAULT_X87_FAULT 0x10
-#define FAULT_ALIGNMENT_CHECK 0x11
-#define ABORT_MACHINE_CHECK 0x12
-#define FAULT_SIMD_FP_EXCEPTION 0x13
-#define FAULT_VIRTUALIZATION_EXCEPTION 0x14
-#define FAULT_CONTROL_PROTECTION 0x15
-
-
-#endif /* __LUNAIX_TYPES_H */
#include <stddef.h>
-void
-lxsbrk(void* current, void* next);
+#define HEAP_INIT_SIZE 4096
-void
-lxmalloc(size_t size);
+int
+dmm_init();
+
+int
+lxsbrk(void* addr);
+void*
+lxbrk(size_t size);
+
+void*
+lx_malloc(size_t size);
void
-lxfree(size_t size);
+lx_free(void* ptr);
#endif /* __LUNAIX_DMM_H */
#include <stdint.h>
#include <lunaix/constants.h>
-#define PG_SIZE_BITS 12
+#define PG_SIZE_BITS 12
+#define PG_SIZE (1 << PG_SIZE_BITS)
#define PG_INDEX_BITS 10
#define PG_MAX_ENTRIES 1024U
void*
vmm_alloc_page(void* va, pt_attr tattr);
+
+/**
+ * @brief 尝试分配多个连续的虚拟页
+ *
+ * @param va 起始虚拟地址
+ * @param sz 大小(必须为4K对齐)
+ * @param tattr 属性
+ * @return int 是否成功
+ */
+int
+vmm_alloc_pages(void* va, size_t sz, pt_attr tattr);
+
/**
* @brief 删除一个映射
*
--- /dev/null
+#ifndef __LUNAIX_SPIKE_H
+#define __LUNAIX_SPIKE_H
+
+// Some helper functions. As helpful as Spike the Dragon! :)
+
+// 除法向上取整
+#define CEIL(v, k) (((v) + (1 << (k)) - 1) >> (k))
+
+// 除法向下取整
+#define FLOOR(v, k) ((v) >> (k))
+
+// 获取v最近的最大k倍数
+#define ROUNDUP(v, k) (((v) + (k) - 1) & ~((k) - 1))
+
+// 获取v最近的最小k倍数
+#define ROUNDDOWN(v, k) ((v) & ~((k) - 1))
+
+static void inline spin() {
+ while(1);
+}
+
+#endif /* __LUNAIX_SPIKE_H */
#include <arch/x86/idt.h>
#include <arch/x86/interrupts.h>
-#include <arch/x86/types.h>
#include <stdint.h>
#define IDT_ENTRY 32
void
_init_idt() {
_set_idt_entry(FAULT_DIVISION_ERROR, 0x08, _asm_isr0, 0);
+ _set_idt_entry(FAULT_GENERAL_PROTECTION, 0x08, _asm_isr13, 0);
}
\ No newline at end of file
.section .text
isr_template 0
+ isr_template 13, no_error_code=0
interrupt_wrapper:
#include <arch/x86/interrupts.h>
+#include <lunaix/assert.h>
#include <libc/stdio.h>
-void isr0 (isr_param* param) {
- printf("[PANIC] Exception (%d) CS=0x%X, EIP=0x%X", param->vector, param->cs, param->eip);
+void panic (const char* msg, isr_param* param) {
+ tty_set_theme(VGA_COLOR_BLACK, VGA_COLOR_LIGHT_RED);
+ tty_clear_line(10);
+ tty_clear_line(11);
+ tty_clear_line(12);
+ tty_set_cpos(0, 11);
+ printf(" INT %u: [0x%x: 0x%x] %s", param->vector, param->cs, param->eip, msg);
+ __spin:
+ goto __spin;
}
void
switch (param->vector)
{
case 0:
- isr0(param);
- break;
+ panic("Division by 0", param);
+ break; // never reach
+ case FAULT_GENERAL_PROTECTION:
+ panic("General Protection", param);
+ break; // never reach
+ default:
+ panic("Unknown Interrupt", param);
+ break; // never reach
}
}
\ No newline at end of file
#include <stdint.h>
#include <lunaix/mm/vmm.h>
+#include <lunaix/mm/dmm.h>
#include <hal/cpu.h>
#include <libc/stdio.h>
void* k_start = vmm_v2p(&__kernel_start);
printf("The kernel's base address mapping: %p->%p\n", &__kernel_start, k_start);
+ dmm_init();
+
+ // test malloc & free
+
+ uint32_t** arr = (uint32_t**) lx_malloc(10 * sizeof(uint32_t*));
+
+ for (size_t i = 0; i < 10; i++)
+ {
+ arr[i] = (uint32_t*) lx_malloc((i + 1) * 2);
+ }
+
+ for (size_t i = 0; i < 10; i++)
+ {
+ lx_free(arr[i]);
+ }
+
+ lx_free(arr);
// assert(0);
}
\ No newline at end of file
+/**
+ * @file dmm.c
+ * @author Lunaixsky
+ * @brief Dynamic memory manager dedicated to kernel heap. It is not portable at
+ * this moment.
+ * @version 0.1
+ * @date 2022-02-28
+ *
+ * @copyright Copyright (c) Lunaixsky 2022
+ *
+ */
+
#include <lunaix/mm/dmm.h>
+#include <lunaix/mm/page.h>
#include <lunaix/mm/vmm.h>
-// This is a temporary design.
-// We can do better when we are ready for multitasking
-void
-lxsbrk(void* current, void* next) {
- // TODO: sbrk
+#include <lunaix/assert.h>
+#include <lunaix/constants.h>
+#include <lunaix/spike.h>
+
+#include <stdbool.h>
+
+#define M_ALLOCATED 0x1
+#define M_PREV_FREE 0x2
+
+#define M_NOT_ALLOCATED 0x0
+#define M_PREV_ALLOCATED 0x0
+
+#define CHUNK_S(header) ((header) & ~0x3)
+#define CHUNK_PF(header) ((header)&M_PREV_FREE)
+#define CHUNK_A(header) ((header)&M_ALLOCATED)
+
+#define PACK(size, flags) (((size) & ~0x3) | (flags))
+
+#define SW(p, w) (*((uint32_t*)(p)) = w)
+#define LW(p) (*((uint32_t*)(p)))
+
+#define HPTR(bp) ((uint32_t*)(bp)-1)
+#define BPTR(bp) ((uint8_t*)(bp) + WSIZE)
+#define FPTR(hp, size) ((uint32_t*)(hp + size - WSIZE))
+#define NEXT_CHK(hp) ((uint8_t*)(hp) + CHUNK_S(LW(hp)))
+
+#define BOUNDARY 4
+#define WSIZE 4
+
+extern uint8_t __kernel_heap_start;
+
+void* current_heap_top = NULL;
+
+void*
+coalesce(uint8_t* chunk_ptr);
+
+void*
+lx_grow_heap(size_t sz);
+
+int
+dmm_init()
+{
+ assert((uintptr_t)&__kernel_heap_start % BOUNDARY == 0);
+
+ current_heap_top = &__kernel_heap_start;
+ uint8_t* heap_start = &__kernel_heap_start;
+
+ vmm_alloc_page(current_heap_top, PG_PREM_RW);
+
+ SW(heap_start, PACK(4, M_ALLOCATED));
+ SW(heap_start + WSIZE, PACK(0, M_ALLOCATED));
+ current_heap_top += WSIZE;
+
+ return lx_grow_heap(HEAP_INIT_SIZE);
}
-void
-lxmalloc(size_t size) {
- // TODO: Malloc
+int
+lxsbrk(void* addr)
+{
+ return lxbrk(addr - current_heap_top) != NULL;
+}
+
+void*
+lxbrk(size_t size)
+{
+ if (size == 0) {
+ return NULL;
+ }
+
+ // plus WSIZE is the overhead for epilogue marker
+ size += WSIZE;
+ void* next = current_heap_top + ROUNDUP((uintptr_t)size, WSIZE);
+
+ if (next >= K_STACK_START) {
+ return NULL;
+ }
+
+ // Check the invariant
+ assert(size % BOUNDARY == 0)
+
+ uintptr_t heap_top_pg = PG_ALIGN(current_heap_top);
+ if (heap_top_pg != PG_ALIGN(next))
+ {
+ // if next do require new pages to be allocated
+ if (!vmm_alloc_pages(heap_top_pg + PG_SIZE, ROUNDUP(size, PG_SIZE), PG_PRESENT | PG_WRITE)) {
+ // TODO: OOM, panic here! Rather than spinning.
+ spin();
+ // return NULL
+ }
+
+ }
+
+ uintptr_t old = current_heap_top;
+ current_heap_top = next - WSIZE;
+ return old;
+}
+
+void*
+lx_grow_heap(size_t sz) {
+ uintptr_t start;
+
+ sz = ROUNDUP(sz, BOUNDARY);
+ if (!(start = lxbrk(sz))) {
+ return NULL;
+ }
+
+ uint32_t old_marker = *((uint32_t*)start);
+ uint32_t free_hdr = PACK(sz, CHUNK_PF(old_marker));
+ SW(start, free_hdr);
+ SW(FPTR(start, sz), free_hdr);
+ SW(NEXT_CHK(start), PACK(0, M_ALLOCATED | M_PREV_FREE));
+
+ return coalesce(start);
+}
+
+void*
+lx_malloc(size_t size)
+{
+ // Simplest first fit approach.
+
+ uint8_t* ptr = &__kernel_heap_start;
+ // round to largest 4B aligned value
+ // and space for header
+ size = ROUNDUP(size, BOUNDARY) + WSIZE;
+ while (ptr < current_heap_top) {
+ uint32_t header = *((uint32_t*)ptr);
+ size_t chunk_size = CHUNK_S(header);
+ if (chunk_size >= size && !CHUNK_A(header)) {
+ // found!
+ *((uint32_t*)ptr) = PACK(size, CHUNK_PF(header) | M_ALLOCATED);
+ uint8_t* n_hdrptr = (uint8_t*)(ptr + size);
+ uint32_t diff = chunk_size - size;
+ if (!diff) {
+ // if the current free block is fully occupied
+ uint32_t n_hdr = LW(n_hdrptr);
+ // notify the next block about our avaliability
+ SW(n_hdrptr, n_hdr & ~0x2);
+ } else {
+ // if there is remaining free space left
+ uint32_t remainder_hdr =
+ PACK(diff, M_NOT_ALLOCATED | M_PREV_ALLOCATED);
+ SW(n_hdrptr, remainder_hdr);
+ SW(FPTR(n_hdrptr, diff), remainder_hdr);
+
+ coalesce(n_hdrptr);
+ }
+ return BPTR(ptr);
+ }
+ ptr += chunk_size;
+ }
+
+ return NULL;
}
void
-lxfree(size_t size) {
- // TODO: Free
+lx_free(void* ptr)
+{
+ uint8_t* chunk_ptr = (uint8_t*)ptr - WSIZE;
+ uint32_t hdr = LW(chunk_ptr);
+ uint8_t* next_hdr = chunk_ptr + CHUNK_S(hdr);
+
+ SW(chunk_ptr, hdr & ~M_ALLOCATED);
+ SW(FPTR(chunk_ptr, CHUNK_S(hdr)), hdr & ~M_ALLOCATED);
+ SW(next_hdr, LW(next_hdr) | M_PREV_FREE);
+
+ coalesce(chunk_ptr);
+}
+
+void*
+coalesce(uint8_t* chunk_ptr)
+{
+ uint32_t hdr = LW(chunk_ptr);
+ uint32_t pf = CHUNK_PF(hdr);
+ uint32_t sz = CHUNK_S(hdr);
+ uint32_t ftr = LW(chunk_ptr + sz - WSIZE);
+
+ uint32_t n_hdr = LW(chunk_ptr + sz);
+
+ if (CHUNK_A(n_hdr) && pf) {
+ // case 1: prev is free
+ uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
+ size_t prev_chunk_sz = CHUNK_S(prev_ftr);
+ uint32_t new_hdr = PACK(prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
+ SW(chunk_ptr - prev_chunk_sz, new_hdr);
+ SW(FPTR(chunk_ptr, sz), new_hdr);
+ chunk_ptr -= prev_chunk_sz;
+ } else if (!CHUNK_A(n_hdr) && !pf) {
+ // case 2: next is free
+ size_t next_chunk_sz = CHUNK_S(n_hdr);
+ uint32_t new_hdr = PACK(next_chunk_sz + sz, pf);
+ SW(chunk_ptr, new_hdr);
+ SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
+ } else if (!CHUNK_A(n_hdr) && pf) {
+ // case 3: both free
+ uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
+ size_t next_chunk_sz = CHUNK_S(n_hdr);
+ size_t prev_chunk_sz = CHUNK_S(prev_ftr);
+ uint32_t new_hdr =
+ PACK(next_chunk_sz + prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
+ SW(chunk_ptr - prev_chunk_sz, new_hdr);
+ SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
+ chunk_ptr -= prev_chunk_sz;
+ }
+
+ // case 4: prev and next are not free
+ return chunk_ptr;
}
\ No newline at end of file
#include <lunaix/assert.h>
#include <hal/cpu.h>
+#include <stdbool.h>
+
void
vmm_init()
{
}
int
-__vmm_map_internal(uint32_t l1_inx, uint32_t l2_inx, uintptr_t pa, pt_attr attr) {
+__vmm_map_internal(uint32_t l1_inx, uint32_t l2_inx, uintptr_t pa, pt_attr attr, int forced) {
ptd_t* l1pt = (ptd_t*)L1_BASE_VADDR;
pt_t* l2pt = (pt_t*)L2_VADDR(l1_inx);
}
l1pt[l1_inx] = NEW_L1_ENTRY(attr, new_l1pt_pa);
- memset((void*)L2_VADDR(l1_inx), 0, PM_PAGE_SIZE);
+ memset((void*)L2_VADDR(l1_inx), 0, PG_SIZE);
+ }
+
+ if (!forced && l2pt[l2_inx]) {
+ return 0;
}
l2pt[l2_inx] = NEW_L2_ENTRY(attr, pa);
return NULL;
}
- if (!__vmm_map_internal(l1_index, l2_index, pa, tattr)) {
+ if (!__vmm_map_internal(l1_index, l2_index, pa, tattr, false)) {
return NULL;
}
uint32_t l1_index = L1_INDEX(va);
uint32_t l2_index = L2_INDEX(va);
- if (!__vmm_map_internal(l1_index, l2_index, pa, tattr)) {
+ if (!__vmm_map_internal(l1_index, l2_index, pa, tattr, true)) {
return NULL;
}
return result;
}
+int
+vmm_alloc_pages(void* va, size_t sz, pt_attr tattr) {
+ assert((uintptr_t)va % PG_SIZE == 0)
+ assert(sz % PG_SIZE == 0)
+
+ void* va_ = va;
+ for (size_t i = 0; i < (sz >> PG_SIZE_BITS); i++, va_ += PG_SIZE)
+ {
+ void* pp = pmm_alloc_page();
+ uint32_t l1_index = L1_INDEX(va_);
+ uint32_t l2_index = L2_INDEX(va_);
+ if (!pp || !__vmm_map_internal(l1_index, l2_index, pp, tattr, false)) {
+ // if one failed, release previous allocated pages.
+ va_ = va;
+ for (size_t j = 0; j < i; j++, va_ += PG_SIZE)
+ {
+ vmm_unmap_page(va_);
+ }
+
+ return false;
+ }
+ }
+
+ return true;
+}
+
void
vmm_unmap_page(void* va)
{
if (l1pte) {
pt_t* l2pt = (pt_t*)L2_VADDR(l1_index);
uint32_t l2pte = l2pt[l2_index];
- if (IS_CACHED(l2pte) && pmm_free_page((void*)l2pte)) {
- cpu_invplg(va);
+ if (IS_CACHED(l2pte)) {
+ pmm_free_page((void*)l2pte);
}
+ cpu_invplg(va);
l2pt[l2_index] = 0;
}
}
}
__kernel_end = ALIGN(4K);
- __heap_start = ALIGN(4K); /* 内核结束的地方即堆开始的地方 */
+ __kernel_heap_start = ALIGN(4K); /* 内核结束的地方即堆开始的地方 */
}
\ No newline at end of file
@$(CC) -T linker.ld -o $(BIN_DIR)/$(OS_BIN) $(SRC) $(LDFLAGS)
$(BUILD_DIR)/$(OS_ISO): $(ISO_DIR) $(BIN_DIR)/$(OS_BIN) GRUB_TEMPLATE
- @./config-grub.sh ${OS_NAME} > $(ISO_GRUB_DIR)/grub.cfg
+ @./config-grub.sh ${OS_NAME} $(ISO_GRUB_DIR)/grub.cfg
@cp $(BIN_DIR)/$(OS_BIN) $(ISO_BOOT_DIR)
@grub-mkrescue -o $(BUILD_DIR)/$(OS_ISO) $(ISO_DIR)
clean:
@rm -rf $(BUILD_DIR)
+ @sleep 1
run: $(BUILD_DIR)/$(OS_ISO)
@qemu-system-i386 -cdrom $(BUILD_DIR)/$(OS_ISO) -monitor telnet::$(QEMU_MON_PORT),server,nowait &