diff options
-rw-r--r-- | apps/Makefile | 2 | ||||
-rw-r--r-- | kernel/Makefile | 2 | ||||
-rw-r--r-- | kernel/drivers/interrupts.c | 2 | ||||
-rw-r--r-- | kernel/features/mm.c | 1 | ||||
-rw-r--r-- | kernel/features/proc.c | 12 | ||||
-rw-r--r-- | kernel/inc/proc.h | 8 | ||||
-rw-r--r-- | libs/libc/Makefile | 4 | ||||
-rw-r--r-- | libs/libc/alloc.c | 4 | ||||
-rw-r--r-- | libs/libc/cpu.c | 2 | ||||
-rw-r--r-- | libs/libc/errno.c | 2 | ||||
-rw-r--r-- | libs/libc/inc/assert.h | 6 | ||||
-rw-r--r-- | libs/libc/inc/cpu.h | 2 | ||||
-rw-r--r-- | libs/libc/inc/def.h | 6 | ||||
-rw-r--r-- | libs/libc/inc/errno.h | 2 | ||||
-rw-r--r-- | libs/libc/inc/mem.h | 6 | ||||
-rw-r--r-- | libs/libc/inc/print.h | 2 | ||||
-rw-r--r-- | libs/libc/inc/sys.h | 2 | ||||
-rw-r--r-- | libs/libc/mem.c | 2 | ||||
-rw-r--r-- | libs/libc/print.c | 4 | ||||
-rw-r--r-- | libs/libc/random.c | 4 | ||||
-rw-r--r-- | libs/libc/sys.c | 2 | ||||
-rw-r--r-- | libs/libgui/Makefile | 2 | ||||
-rw-r--r-- | libs/libnet/Makefile | 2 | ||||
-rw-r--r-- | libs/libtxt/Makefile | 2 |
24 files changed, 45 insertions, 38 deletions
diff --git a/apps/Makefile b/apps/Makefile index f689727..c015d18 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -6,7 +6,7 @@ LD = ccache ../cross/opt/bin/i686-elf-ld OC = ccache ../cross/opt/bin/i686-elf-objcopy ST = ccache ../cross/opt/bin/i686-elf-strip -CFLAGS = $(CFLAGS_DEFAULT) -I../libs/ -I../libs/libc/inc/ -Duserspace +CFLAGS = $(CFLAGS_DEFAULT) -I../libs/ -I../libs/libc/inc/ -DUSER all: $(COBJS) diff --git a/kernel/Makefile b/kernel/Makefile index e9c7b09..392665d 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -23,7 +23,7 @@ LD = ccache ../cross/opt/bin/i686-elf-ld OC = ccache ../cross/opt/bin/i686-elf-objcopy AS = ccache nasm -CFLAGS = $(CFLAGS_DEFAULT) -Wno-address-of-packed-member -ffreestanding -Wl,-ekernel_main -I../libs/libc/inc/ -Iinc/ -Dkernel +CFLAGS = $(CFLAGS_DEFAULT) -Wno-address-of-packed-member -ffreestanding -Wl,-ekernel_main -I../libs/libc/inc/ -Iinc/ -DKERNEL ASFLAGS = -f elf32 all: compile diff --git a/kernel/drivers/interrupts.c b/kernel/drivers/interrupts.c index 917733f..0189b6c 100644 --- a/kernel/drivers/interrupts.c +++ b/kernel/drivers/interrupts.c @@ -173,7 +173,7 @@ void isr_uninstall_handler(int isr) void isr_panic(struct regs *r) { printf("%s Exception (%x) at 0x%x (ring %d), exiting!\n", isr_exceptions[r->int_no], - r->err_code, r->eip, r->cs & 3); + r->err_code, r->eip, RING(r)); struct proc *proc = proc_current(); if (proc) { printf("\t-> Exception occurred in %s at addr 0x%x (offset 0x%x)\n", proc->name, diff --git a/kernel/features/mm.c b/kernel/features/mm.c index afee468..064f78c 100644 --- a/kernel/features/mm.c +++ b/kernel/features/mm.c @@ -377,7 +377,6 @@ void *memory_alloc_identity(struct page_dir *dir, u32 flags) return NULL; } -// TODO: Free by address instead of vrange (combine with shmem map?) void memory_free(struct page_dir *dir, struct memory_range vrange) { assert(PAGE_ALIGNED(vrange.base) && PAGE_ALIGNED(vrange.size)); diff --git a/kernel/features/proc.c b/kernel/features/proc.c index 2b2d2d2..8625e7d 100644 --- a/kernel/features/proc.c +++ b/kernel/features/proc.c @@ -31,7 +31,10 @@ HOT FLATTEN void scheduler(struct regs *regs) { spinlock(&locked); - PROC(current)->ticks++; + if (RING(regs) == 3) + PROC(current)->ticks.user++; + else + PROC(current)->ticks.kernel++; if (PROC(current)->quantum.cnt >= PROC(current)->quantum.val) { PROC(current)->quantum.cnt = 0; @@ -93,14 +96,14 @@ struct proc *proc_from_pid(u32 pid) iterator = proc_list_blocked->head; while (iterator) { - if (((struct proc *)iterator->data)->pid == pid) + if (PROC(iterator)->pid == pid) return iterator->data; iterator = iterator->next; } iterator = proc_list_running->head; while (iterator) { - if (((struct proc *)iterator->data)->pid == pid) + if (PROC(iterator)->pid == pid) return iterator->data; iterator = iterator->next; } @@ -152,12 +155,15 @@ void proc_exit(struct proc *proc, struct regs *r, s32 status) if (proc->memory->head) { printf("Process leaked memory:\n"); + u32 total = 0; struct node *iterator = proc->memory->head; while (iterator) { struct memory_proc_link *link = iterator->data; printf("\t-> 0x%x: %dB\n", link->vrange.base, link->vrange.size); + total += link->vrange.size; iterator = iterator->next; } + printf("\tTOTAL: %dB (%dKiB)\n", total, total >> 10); } else { printf("Process didn't leak memory!\n"); } diff --git a/kernel/inc/proc.h b/kernel/inc/proc.h index c9dadaa..8a172c9 100644 --- a/kernel/inc/proc.h +++ b/kernel/inc/proc.h @@ -19,6 +19,8 @@ #define GDT_USER_CODE_OFFSET 0x1b // User code segment offset in GDT (with ring3 mask) #define GDT_USER_DATA_OFFSET 0x23 // User data segment offset in GDT (with ring3 mask) +#define RING(regs) ((regs->cs) & 3) + #define PROC_MAX_BLOCK_IDS 16 #define PROC_BLOCK_MAGIC 0x00528491 @@ -60,7 +62,11 @@ struct proc { struct stack *messages; struct list *memory; - u32 ticks; + struct { + u32 user; + u32 kernel; + } ticks; + struct { u8 val; u8 cnt; diff --git a/libs/libc/Makefile b/libs/libc/Makefile index 1c66f43..d8d58ff 100644 --- a/libs/libc/Makefile +++ b/libs/libc/Makefile @@ -27,13 +27,13 @@ ASFLAGS = -f elf32 %.o: %.c @$(CC) -c $(CFLAGS) $< -o $@ -libc: CFLAGS += -Duserspace +libc: CFLAGS += -DUSER libc: $(COBJS) @$(AS) $(ASFLAGS) crt/crt0.asm -o crt0.o @mkdir -p ../../build/ @$(AR) rcs ../../build/libc.a crt0.o $+ -libk: CFLAGS += -Dkernel -ffreestanding -I../../kernel/inc/ $(CFLAGS_EXTRA) +libk: CFLAGS += -DKERNEL -ffreestanding -I../../kernel/inc/ $(CFLAGS_EXTRA) libk: $(COBJS) @mkdir -p ../../build/ @$(AR) rcs ../../build/libk.a $+ diff --git a/libs/libc/alloc.c b/libs/libc/alloc.c index 928a519..02e30eb 100644 --- a/libs/libc/alloc.c +++ b/libs/libc/alloc.c @@ -5,7 +5,7 @@ #include <cpu.h> #include <mem.h> -#ifdef kernel +#ifdef KERNEL #include <mm.h> @@ -379,7 +379,7 @@ static void *_realloc(void *ptr, u32 size) return new_ptr; } -#ifdef kernel +#ifdef KERNEL #define PREFIX "K" #define FUNC printf #else diff --git a/libs/libc/cpu.c b/libs/libc/cpu.c index 37bdb4d..7fbb66a 100644 --- a/libs/libc/cpu.c +++ b/libs/libc/cpu.c @@ -48,7 +48,7 @@ void outl(u16 port, u32 data) __asm__ volatile("outl %0, %1" ::"a"(data), "Nd"(port)); } -#ifdef kernel +#ifdef KERNEL static void cpuid(int code, u32 *a, u32 *b, u32 *c, u32 *d) { diff --git a/libs/libc/errno.c b/libs/libc/errno.c index 7204f5b..06d2b33 100644 --- a/libs/libc/errno.c +++ b/libs/libc/errno.c @@ -2,7 +2,7 @@ #include <def.h> #include <errno.h> -#ifdef userspace +#ifdef USER static u32 error = 0; diff --git a/libs/libc/inc/assert.h b/libs/libc/inc/assert.h index ed835c5..9621e36 100644 --- a/libs/libc/inc/assert.h +++ b/libs/libc/inc/assert.h @@ -5,7 +5,7 @@ #include <print.h> -#ifdef kernel +#ifdef KERNEL #include <proc.h> #define assert(exp) \ if (!(exp)) { \ @@ -13,12 +13,10 @@ #exp); \ __asm__ volatile("cli\nhlt"); \ } -#elif defined(userspace) +#elif defined(USER) #define assert(exp) \ if (!(exp)) \ err(1, "%s:%d: %s: Assertion '%s' failed\n", __FILE__, __LINE__, __func__, #exp); -#else -#error "No lib target specified. Please use -Dkernel or -Duserspace" #endif #endif diff --git a/libs/libc/inc/cpu.h b/libs/libc/inc/cpu.h index 4c6e721..0410125 100644 --- a/libs/libc/inc/cpu.h +++ b/libs/libc/inc/cpu.h @@ -22,7 +22,7 @@ static inline void spinlock(u32 *ptr) while (prev); } -#ifdef kernel +#ifdef KERNEL void cpu_print(void); void cpu_enable_features(void); void fpu_restore(void); diff --git a/libs/libc/inc/def.h b/libs/libc/inc/def.h index 2bf50b1..583a351 100644 --- a/libs/libc/inc/def.h +++ b/libs/libc/inc/def.h @@ -25,10 +25,10 @@ typedef unsigned long long u64; #define UNUSED(__a) ((void)(__a)) -#define MIN(__a, __b) (((__a) < (__b)) ? (__a) : (__b)) -#define MAX(__a, __b) (((__a) > (__b)) ? (__a) : (__b)) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) -#define ABS(__a) ((u32)(((s32)(__a) < 0) ? (-__a) : (__a))) +#define ABS(a) ((u32)(((s32)(a) < 0) ? -(a) : (a))) #define ATTR __attribute__ #define NORETURN ATTR((noreturn)) diff --git a/libs/libc/inc/errno.h b/libs/libc/inc/errno.h index 133f543..787aab1 100644 --- a/libs/libc/inc/errno.h +++ b/libs/libc/inc/errno.h @@ -44,7 +44,7 @@ typedef s32 res; #define ERANGE 34 /* Math result not representable */ #define EMAX 35 /* Max errno */ -#ifdef userspace +#ifdef USER #define errno (*__errno()) extern u32 *__errno(void); diff --git a/libs/libc/inc/mem.h b/libs/libc/inc/mem.h index 2d55eff..960ee86 100644 --- a/libs/libc/inc/mem.h +++ b/libs/libc/inc/mem.h @@ -12,12 +12,10 @@ void free_debug(void *ptr, const char *file, int line, const char *func, const c void *realloc(void *ptr, u32 size); void *zalloc(u32 size); -#ifdef kernel +#ifdef KERNEL #define STACK_START 0x00500000 // Defined it bootloader #define STACK_SIZE 0x1000 // idk -#elif defined(userspace) -#else -#error "No lib target specified. Please use -Dkernel or -Duserspace" +#elif defined(USER) #endif void *memcpy(void *dest, const void *src, u32 n) NONNULL; diff --git a/libs/libc/inc/print.h b/libs/libc/inc/print.h index 751a929..c0d864e 100644 --- a/libs/libc/inc/print.h +++ b/libs/libc/inc/print.h @@ -13,7 +13,7 @@ int vsnprintf(char *str, u32 size, const char *format, va_list ap) NONNULL; int print(const char *str) NONNULL; NORETURN void panic(const char *format, ...) NONNULL; -#ifdef userspace +#ifdef USER int vfprintf(const char *path, const char *format, va_list ap) NONNULL; int fprintf(const char *path, const char *format, ...) NONNULL; int log(const char *format, ...) NONNULL; diff --git a/libs/libc/inc/sys.h b/libs/libc/inc/sys.h index 6565e10..81de724 100644 --- a/libs/libc/inc/sys.h +++ b/libs/libc/inc/sys.h @@ -59,7 +59,7 @@ struct stat { u32 size; }; -#if defined(userspace) +#ifdef USER /** * Syscall wrappers diff --git a/libs/libc/mem.c b/libs/libc/mem.c index 2e457ef..ad691ed 100644 --- a/libs/libc/mem.c +++ b/libs/libc/mem.c @@ -7,7 +7,7 @@ void *memcpy(void *dest, const void *src, u32 n) { -#ifdef userspace +#ifdef USER // Inspired by Jeko at osdev u8 *dest_byte = dest; const u8 *src_byte = src; diff --git a/libs/libc/print.c b/libs/libc/print.c index d990985..b77c165 100644 --- a/libs/libc/print.c +++ b/libs/libc/print.c @@ -96,7 +96,7 @@ int snprintf(char *str, u32 size, const char *format, ...) return len; } -#ifdef userspace +#ifdef USER #include <sys.h> #define PATH_OUT "/proc/self/io/out" @@ -247,7 +247,7 @@ NORETURN void panic(const char *format, ...) va_start(ap, format); vsnprintf(buf, sizeof(buf), format, ap); va_end(ap); -#ifdef kernel +#ifdef KERNEL print("--- DON'T PANIC! ---\n"); print(buf); print_trace(5); diff --git a/libs/libc/random.c b/libs/libc/random.c index 2801029..5cbbbb6 100644 --- a/libs/libc/random.c +++ b/libs/libc/random.c @@ -14,7 +14,7 @@ void srand(u32 seed) u32 rdrand(void) { -#ifdef kernel +#ifdef KERNEL if (!cpu_has_cfeature(CPUID_FEAT_ECX_RDRND)) return rand(); @@ -31,7 +31,7 @@ u32 rdrand(void) u32 rdseed(void) { -#ifdef kernel +#ifdef KERNEL if (!cpu_has_cfeature(CPUID_FEAT_ECX_RDRND)) return rand(); diff --git a/libs/libc/sys.c b/libs/libc/sys.c index 491c37c..15a9669 100644 --- a/libs/libc/sys.c +++ b/libs/libc/sys.c @@ -6,7 +6,7 @@ #include <errno.h> #include <sys.h> -#if defined(userspace) +#ifdef USER /** * Definitions diff --git a/libs/libgui/Makefile b/libs/libgui/Makefile index 3e2b0b2..e04b703 100644 --- a/libs/libgui/Makefile +++ b/libs/libgui/Makefile @@ -10,7 +10,7 @@ CC = ccache ../../cross/opt/bin/i686-elf-gcc LD = ccache ../../cross/opt/bin/i686-elf-ld AR = ccache ../../cross/opt/bin/i686-elf-ar -CFLAGS = $(CFLAGS_DEFAULT) -I../ -I../libc/inc/ -Duserspace +CFLAGS = $(CFLAGS_DEFAULT) -I../ -I../libc/inc/ -DUSER all: libgui diff --git a/libs/libnet/Makefile b/libs/libnet/Makefile index 85c8fab..69c899d 100644 --- a/libs/libnet/Makefile +++ b/libs/libnet/Makefile @@ -5,7 +5,7 @@ CC = ccache ../../cross/opt/bin/i686-elf-gcc LD = ccache ../../cross/opt/bin/i686-elf-ld AR = ccache ../../cross/opt/bin/i686-elf-ar -CFLAGS = $(CFLAGS_DEFAULT) -I../ -I../libc/inc/ -Duserspace +CFLAGS = $(CFLAGS_DEFAULT) -I../ -I../libc/inc/ -DUSER all: libtxt diff --git a/libs/libtxt/Makefile b/libs/libtxt/Makefile index 93a7993..6d06502 100644 --- a/libs/libtxt/Makefile +++ b/libs/libtxt/Makefile @@ -5,7 +5,7 @@ CC = ccache ../../cross/opt/bin/i686-elf-gcc LD = ccache ../../cross/opt/bin/i686-elf-ld AR = ccache ../../cross/opt/bin/i686-elf-ar -CFLAGS = $(CFLAGS_DEFAULT) -I../ -I../libc/inc/ -Duserspace +CFLAGS = $(CFLAGS_DEFAULT) -I../ -I../libc/inc/ -DUSER all: libtxt |