diff --git a/kern/ispinlock.h b/kern/ispinlock.h index d71af17..e30d4b6 100644 --- a/kern/ispinlock.h +++ b/kern/ispinlock.h @@ -1,5 +1,5 @@ #pragma once -#include +#include typedef struct { volatile uint32_t v; // 0 = unlocked, 1 = locked diff --git a/kern/proc.h b/kern/proc.h index b594fc9..26659f3 100644 --- a/kern/proc.h +++ b/kern/proc.h @@ -1,3 +1,6 @@ +#include +#include +#include #include typedef enum { @@ -37,6 +40,7 @@ struct Cpu { int intena; // Were interrupts enabled before push_off()? }; +/** Saved registers for kernel context switches. */ typedef struct { /* 0 */ uint64_t kernel_satp; // kernel page table /* 8 */ uint64_t kernel_sp; // top of process's kernel stack @@ -75,3 +79,10 @@ typedef struct { /* 272 */ uint64_t t5; /* 280 */ uint64_t t6; } TrapFrame_t; + +struct Cpu *mycpu(void); + +extern struct Cpu cpus[NCPU]; + +/** Per-process state */ +struct Proc {}; diff --git a/lib/proc.c b/lib/proc.c deleted file mode 100644 index 66bca67..0000000 --- a/lib/proc.c +++ /dev/null @@ -1,21 +0,0 @@ -#include - -struct Cpu cpus[NCPU]; - -/** - * Must be called with interrupts disabled, to prevent race with process being - * moved to a different CPU. - */ -int cpuid() { - int id = read_tp(); - return id; -} - -/** - * Return this CPU's cpu struct. Interrupts must be disabled. - */ -struct Cpu *mycpu(void) { - int id = cpuid(); - struct Cpu *c = &cpus[id]; - return c; -} diff --git a/lib/proc.h b/lib/proc.h deleted file mode 100644 index 1208b2b..0000000 --- a/lib/proc.h +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include -#include -#include - -struct Cpu *mycpu(void); - -/** Saved registers for kernel context switches. */ -struct Context {}; - -/** Per-CPU state. */ -struct Cpu { - struct Proc *proc; // The process running on this cpu, or null. - struct Context context; // swtch() here to enter scheduler(). - int noff; // Depth of push_off() nesting. - int intena; // Were interrupts enabled before push_off()? -}; - -extern struct Cpu cpus[NCPU]; - -/** Per-process state */ -struct Proc {}; diff --git a/lib/string.c b/lib/string.c deleted file mode 100644 index 69fb5c7..0000000 --- a/lib/string.c +++ /dev/null @@ -1,99 +0,0 @@ -#include - -char *itoa(int value, char *str, int base) { - char *p = str; - char *p1, *p2; - unsigned int uvalue = value; - int negative = 0; - - if (base < 2 || base > 36) { - *str = '\0'; - return str; - } - - if (value < 0 && base == 10) { - negative = 1; - uvalue = -value; - } - - // Convert to string - do { - int digit = uvalue % base; - *p++ = (digit < 10) ? '0' + digit : 'a' + (digit - 10); - uvalue /= base; - } while (uvalue); - - if (negative) - *p++ = '-'; - - *p = '\0'; - - // Reverse string - p1 = str; - p2 = p - 1; - while (p1 < p2) { - char tmp = *p1; - *p1++ = *p2; - *p2-- = tmp; - } - - return str; -} - -void *memset(void *dst, int c, size_t length) { - u8 *ptr = (u8 *)dst; - const u8 value = (u8)c; - - while (length--) *(ptr++) = value; - - return dst; -} - -void *memcpy(void *dst, const void *src, size_t len) { - u8 *d = (u8 *)dst; - const u8 *s = (const u8 *)src; - for (size_t i = 0; i < len; i++) { - d[i] = s[i]; - } - return dst; -} - -void *memmove(void *dst, const void *src, size_t len) { - u8 *d = (u8 *)dst; - const u8 *s = (const u8 *)src; - if (d < s) { - for (size_t i = 0; i < len; i++) { - d[i] = s[i]; - } - } else if (d > s) { - for (size_t i = len; i > 0; i--) { - d[i - 1] = s[i - 1]; - } - } - return dst; -} - -int memcmp(const void *s1, const void *s2, size_t len) { - const u8 *a = (const u8 *)s1; - const u8 *b = (const u8 *)s2; - for (size_t i = 0; i < len; i++) { - if (a[i] != b[i]) { - return (int)a[i] - (int)b[i]; - } - } - return 0; -} - -size_t strlen(const char *s) { - const char *p = s; - while (*p) ++p; - return (size_t)(p - s); -} - -size_t strnlen(const char *s, size_t maxlen) { - size_t len = 0; - while (len < maxlen && s[len] != '\0') { - len++; - } - return len; -} diff --git a/lib/string.h b/lib/string.h deleted file mode 100644 index 00805c7..0000000 --- a/lib/string.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef KERNEL_STRING_H -#define KERNEL_STRING_H - -#include - -/** Integer to ascii */ -char *itoa(int value, char *str, int base); - -/** Fill memory with constant byte */ -void *memset(void *dst, int c, size_t len); - -/** Copy `len` bytes from `src` to `dst`. Undefined if regions overlap. */ -void *memcpy(void *dst, const void *src, size_t len); - -/** Copy `len` bytes from `src` to `dst`, safe for overlapping regions. */ -void *memmove(void *dst, const void *src, size_t len); - -/** Compare `len` bytes of `s1` and `s2`. - * Returns 0 if equal, <0 if s1 < s2, >0 if s1 > s2. */ -int memcmp(const void *s1, const void *s2, size_t len); - -/** Returns the length of a null-terminated string */ -size_t strlen(const char *s); - -/** Return length of string `s`, up to a max of `maxlen` bytes */ -size_t strnlen(const char *s, size_t maxlen); - -// TODO: These: -/* -int strcmp(const char *s1, const char *s2); -int strncmp(const char *s1, const char *s2, size_t n); - -char *strcpy(char *dst, const char *src); -char *strncpy(char *dst, const char *src, size_t n); - -char *strchr(const char *s, int c); -char *strrchr(const char *s, int c); -*/ - -#endif diff --git a/lib/uart.c b/lib/uart.c deleted file mode 100644 index 83b2656..0000000 --- a/lib/uart.c +++ /dev/null @@ -1,8 +0,0 @@ -/* QEMU memory maps a UART device here. */ -#define UART_BASE ((volatile char *)0x10000000) - -void uart_putc(char c) { *UART_BASE = c; } - -void uart_puts(const char *s) { - while (*s) uart_putc(*s++); -} diff --git a/lib/uart.h b/lib/uart.h deleted file mode 100644 index 953c5f6..0000000 --- a/lib/uart.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef UART_KERNEL_H -#define UART_KERNEL_H - -/** Send a single character to the UART device */ -void uart_putc(char c); - -/** Send a **NULL TERMINATED** string to the UART device */ -void uart_puts(const char *s); - -#endif