Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ define _run_qemu_base
$(QEMU) -M raspi3 \
-drive if=sd,file=./res/sdcard/sfn_nctuos.img,format=raw \
-initrd $(INIT_RAM_FS) \
-d int\
-display none
endef

Expand Down
2 changes: 1 addition & 1 deletion impl-c/dev/mbr.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "dev/mbr.h"

#include "dev/sd.h"
#include "mm.h"
#include "memory.h"
#include "stdint.h"
#include "uart.h"

Expand Down
11 changes: 7 additions & 4 deletions impl-c/dev/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ void uart_println(char *fmt, ...) {
int64_t i;
char *s;
char c;
uint64_t ui;

va_list arg;
va_start(arg, fmt);
Expand All @@ -163,9 +164,9 @@ void uart_println(char *fmt, ...) {
uart_puts(itoa(i, 10));
break;
case 'x':
i = va_arg(arg, int);
ui = va_arg(arg, uint64_t);
uart_puts("0x");
uart_puts(itoa((int64_t)i, 16));
uart_puts(itoa((uint64_t)ui, 16));
break;
case 'c':
c = va_arg(arg, int);
Expand All @@ -187,6 +188,8 @@ void uart_println(char *fmt, ...) {
// same as println, but without a newline at the end
void uart_printf(char *fmt, ...) {
int64_t i;
uint64_t ui;

char *s;
char c;

Expand All @@ -211,9 +214,9 @@ void uart_printf(char *fmt, ...) {
uart_puts(itoa(i, 10));
break;
case 'x':
i = va_arg(arg, int);
ui = va_arg(arg, uint64_t);
uart_puts("0x");
uart_puts(itoa((int64_t)i, 16));
uart_puts(itoa(ui, 16));
break;
case 'c':
c = va_arg(arg, int);
Expand Down
2 changes: 1 addition & 1 deletion impl-c/fs/fat.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "dev/sd.h"

#include "fatal.h"
#include "memory.h"
#include "minmax.h"
#include "mm.h"
#include "stdint.h"
#include "string.h"
#include "uart.h"
Expand Down
2 changes: 1 addition & 1 deletion impl-c/fs/tmpfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include "fs/vfs.h"

#include "dev/cpio.h"
#include "memory.h"
#include "minmax.h"
#include "mm.h"
#include "stdint.h"
#include "string.h"
#include "uart.h"
Expand Down
2 changes: 1 addition & 1 deletion impl-c/fs/vfs.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "fs/vfs.h"

#include "mm.h"
#include "memory.h"
#include "string.h"
#include "uart.h"

Expand Down
3 changes: 2 additions & 1 deletion impl-c/gdbinit
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ file impl-c/kernel8.elf
dir impl-c
target remote :1234


tui enable
si
si
23 changes: 12 additions & 11 deletions impl-c/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
// #define CFG_LOG_SHELL_CMD
// #define CFG_LOG_SHELL_BUFFER
// #define CFG_LOG_MEM_STARTUP
// #define CFG_LOG_MEM_KALLOC
// #define CFG_LOG_MEM_BUDDY
// #define CFG_LOG_MEM_SLAB
// #define CFG_LOG_PROC_TASK
// #define CFG_LOG_PROC_SCHED
// #define CFG_LOG_PROC_ARGV
// #define CFG_LOG_PROC_EXEC
#define CFG_LOG_MEM_KALLOC
#define CFG_LOG_MEM_BUDDY
#define CFG_LOG_MEM_SLAB
#define CFG_LOG_PROC_TASK
#define CFG_LOG_PROC_SCHED
#define CFG_LOG_PROC_ARGV
#define CFG_LOG_PROC_EXEC
#define CFG_LOG_VFS
// #define CFG_LOG_TMPFS
#define CFG_LOG_TMPFS_LOOKUP
#define CFG_LOG_TMPFS_DUMP_TREE
#define CFG_LOG_DEV_MBR
#define CFG_LOG_FAT
#define CFG_LOG_VM

/**
* TEST
Expand All @@ -34,14 +35,14 @@
#define CFG_RUN_LIB_STRING_TEST

// Shell
// #define CFG_RUN_SHELL_BUFFER_TEST
// #define CFG_RUN_SHELL_CMD_TEST
#define CFG_RUN_SHELL_BUFFER_TEST
#define CFG_RUN_SHELL_CMD_TEST

// Memory management
// #define CFG_RUN_STATUP_ALLOC_TEST
#define CFG_RUN_STATUP_ALLOC_TEST

// PROC
// #define CFG_RUN_PROC_ARGV_TEST
#define CFG_RUN_PROC_ARGV_TEST

// DEV
#define CFG_RUN_DEV_MBR_TEST
Expand Down
4 changes: 2 additions & 2 deletions impl-c/include/dev/mmio.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once

#define MMIO_BASE 0x3F000000
#include "mm/vm.h"
#define MMIO_BASE (KVA_START + 0x3F000000)
9 changes: 9 additions & 0 deletions impl-c/include/libs/const.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#define CONST_1K 1024
#define CONST_4K (4 * CONST_1K)

#define CONST_1M (CONST_1K * CONST_1K)
#define CONST_2M (2 * CONST_1M)

#define CONST_1G (CONST_1K * CONST_1M)
2 changes: 1 addition & 1 deletion impl-c/include/libs/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ char *strcpy(char *dst, const char *src);
void memcpy(char *dst, const char *src, size_t n);
const char *strchr(const char *s, const char c);

char *itoa(int64_t val, int base);
char *itoa(uint64_t val, int base);

// Return the fitst char in `s` not being `c`
const char *ignore_leading(const char *s, const char c);
Expand Down
19 changes: 19 additions & 0 deletions impl-c/include/memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

/**
* Memory.h
* Provide interface for dynamic memory allocation
*/

// Allocate a block of memory to use in kernel space (KVA)
void *kalloc(int size);

// Free a memory block (KVA)
void kfree(void *addr);

// Initialize dynamic memory allocator
void KAllocManager_init();
void KAllocManager_show_status();

// Run several allocation/free as an example
void KAllocManager_run_example();
31 changes: 29 additions & 2 deletions impl-c/include/mm/alloc.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
#pragma once
/**
* Buddy & slab allocators
*/

#include "list.h"
#include "mm/const.h"
#include "mm/frame.h"
#include "mm/startup.h"
#include "mm/vm.h"

#include "list.h"
#include <stdint.h>

#define SLAB_NUM_SLAB_SIZES 6

#define BUDDY_MAX_EXPONENT 18 // 1GB
// #define BUDDY_MAX_EXPONENT 10
// #define BUDDY_MAX_EXPONENT 5

#define BUDDY_NUM_FREE_LISTS (BUDDY_MAX_EXPONENT + 1)
#define MEMORY_START KVA_START

// SlabAllocator
// manage slabs with the same allocation size,
// A slab is a frame allocated with small objects with same size
// + @SLAB_MAX_SLOTS: The number of pages available for a single slab
// -> Frame is 4kb by architecture.
// CortexA53 is 16 bytes aligned, so the here 16 bytes is set to be
// the minimum size available for objects in slab.
// -> Therefore, the maximum slots in slab is 4096/16 = 256
#define SLAB_MAX_SLOTS 256

// The size of slab range from 16(2^4) to 512(2^9) bytes
#define SLAB_OBJ_MIN_SIZE_EXP 4
#define SLAB_OBJ_MAX_SIZE_EXP 9

typedef struct Frame {
// inherit a list type, so we could cast FrameNode into list_head
struct list_head list_base;
Expand Down
26 changes: 0 additions & 26 deletions impl-c/include/mm/const.h

This file was deleted.

4 changes: 4 additions & 0 deletions impl-c/include/mm/frame.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#pragma once
/**
* frame.h
* Prvide constants about physical memory frames
*/

#define FRAME_SHIFT 12 // 4Kb
#define FRAME_ADDR_BASE (1 << FRAME_SHIFT)
Expand Down
20 changes: 3 additions & 17 deletions impl-c/include/mm.h → impl-c/include/mm/kalloc_manager.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include "list.h"
#include "mm/alloc.h"
#include "mm/const.h"
#include "mm/frame.h"
#include "mm/startup.h"

#include "list.h"
#include <stdint.h>

/**
Expand All @@ -24,18 +24,4 @@ typedef struct AllocationManager {

// Statically linked to the heap space
// because their lifetimes is equal to the system itself
extern struct Frame Frames[1 << BUDDY_MAX_EXPONENT];

// Allocate a memory space to use in kernel space
void *kalloc(int size);

// Free a memory space
void kfree(void *addr);

// Initialize dynamic memory allocator
void KAllocManager_init();

void KAllocManager_show_status();

// Run several allocation/free as an example
void KAllocManager_run_example();
extern struct Frame Frames[1 << BUDDY_MAX_EXPONENT];
Loading