From aaa1e810e6b6a6e4cab5a739def087631750de17 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Tue, 8 Jun 2021 12:47:45 +0800 Subject: [PATCH] test/nvmm/demo: Various cleanups to 'smallkern' * Remove unused variables, symbols, function prototypes and functions. * Move function prototypes and 'extern' declarations to header files. * Add 'static' qualifier for file-local variables. * Add inclusion guard to header files. * Various minor adjustments. --- test/nvmm/demo/smallkern/Makefile | 2 +- test/nvmm/demo/smallkern/console.c | 2 -- test/nvmm/demo/smallkern/locore.S | 19 ++--------- test/nvmm/demo/smallkern/main.c | 31 ++++-------------- test/nvmm/demo/smallkern/pdir.h | 18 +++++++---- test/nvmm/demo/smallkern/smallkern.h | 47 +++++++++++++++++++--------- 6 files changed, 54 insertions(+), 65 deletions(-) diff --git a/test/nvmm/demo/smallkern/Makefile b/test/nvmm/demo/smallkern/Makefile index f96b1197f3..c535e32ed1 100644 --- a/test/nvmm/demo/smallkern/Makefile +++ b/test/nvmm/demo/smallkern/Makefile @@ -1,7 +1,7 @@ all: cc -Wall -Wextra -g -std=c99 \ -mno-red-zone -ffreestanding \ - -DKERNEL -D__x86_64__ \ + -D__x86_64__ \ -c locore.S trap.S main.c console.c ld -o smallkern \ -X -z max-page-size=0x100000 -Ttext 0x100000 \ diff --git a/test/nvmm/demo/smallkern/console.c b/test/nvmm/demo/smallkern/console.c index ffb080cf49..4a0b834f19 100644 --- a/test/nvmm/demo/smallkern/console.c +++ b/test/nvmm/demo/smallkern/console.c @@ -28,8 +28,6 @@ #include "smallkern.h" -void outsb(int port, char *buf, size_t size); - void print_ext(int color __unused, char *buf) { size_t i; diff --git a/test/nvmm/demo/smallkern/locore.S b/test/nvmm/demo/smallkern/locore.S index 61fd78b844..1969ca612b 100644 --- a/test/nvmm/demo/smallkern/locore.S +++ b/test/nvmm/demo/smallkern/locore.S @@ -40,16 +40,12 @@ #include #include #include -#include -#define _KERNEL -#include -#undef _KERNEL #include "pdir.h" /* 32bit version of PG_NX */ -#define PG_NX32 0x80000000 +#define PG_NX32 0x80000000 #define TABLE_L2_ENTRIES (NKL2_KIMG_ENTRIES + 1) #define TABLE_L3_ENTRIES NKL3_KIMG_ENTRIES @@ -120,11 +116,8 @@ .globl _C_LABEL(nox_flag) .globl _C_LABEL(cpuid_level) .globl _C_LABEL(PDPpaddr) - .globl _C_LABEL(boothowto) - .globl _C_LABEL(bootinfo) - .globl _C_LABEL(biosbasemem) - .globl _C_LABEL(biosextmem) .globl _C_LABEL(atdevbase) + .globl _C_LABEL(lapicbase) .globl _C_LABEL(stkpa) .globl _C_LABEL(stkva) @@ -453,14 +446,6 @@ ENTRY(lidt) ret END(lidt) -ENTRY(rdtsc) - xorq %rax,%rax - rdtsc - shlq $32,%rdx - orq %rdx,%rax - ret -END(rdtsc) - ENTRY(vmmcall) vmmcall END(vmmcall) diff --git a/test/nvmm/demo/smallkern/main.c b/test/nvmm/demo/smallkern/main.c index c34f7cd7aa..71897a60c3 100644 --- a/test/nvmm/demo/smallkern/main.c +++ b/test/nvmm/demo/smallkern/main.c @@ -27,36 +27,21 @@ */ #include "smallkern.h" +#include "pdir.h" +#include "trap.h" #include #include #include - -#define _KERNEL -#include -#undef _KERNEL - #include #include -extern uint32_t nox_flag; -extern uint64_t *gdt64_start; -extern vaddr_t lapicbase; -uint8_t idtstore[PAGE_SIZE]; -uint8_t faultstack[PAGE_SIZE]; -struct x86_64_tss smallkern_tss; - /* GDT offsets */ #define SMALLKERN_GDT_NUL_OFF (0 * 8) #define SMALLKERN_GDT_CS_OFF (1 * 8) #define SMALLKERN_GDT_DS_OFF (2 * 8) #define SMALLKERN_GDT_TSS_OFF (3 * 8) -#define IDTVEC(name) __CONCAT(X, name) -typedef void (vector)(void); -extern vector *x86_exceptions[]; -extern uint64_t Xintr; - void fatal(char *msg) { print("\n"); @@ -88,14 +73,6 @@ static void set_sys_gdt(int, void *, size_t, int, int, int); static void init_tss(void); static void init_idt(void); -void trap(struct smallframe *); -void vmmcall(void); -void clts(void); -void sti(void); -void lcr8(uint64_t); -uint64_t rdmsr(uint64_t); -void cpuid(uint32_t, uint32_t, uint32_t *); - static char *trap_type[] = { "privileged instruction fault", /* 0 T_PRIVINFLT */ "breakpoint trap", /* 1 T_BPTFLT */ @@ -121,6 +98,10 @@ static char *trap_type[] = { }; size_t trap_types = __arraycount(trap_type); +static uint8_t idtstore[PAGE_SIZE] __aligned(PAGE_SIZE); +static uint8_t faultstack[PAGE_SIZE] __aligned(PAGE_SIZE); +static struct x86_64_tss smallkern_tss; + static void triple_fault(void) { diff --git a/test/nvmm/demo/smallkern/pdir.h b/test/nvmm/demo/smallkern/pdir.h index 3098b7eaac..c6f9c5fcdd 100644 --- a/test/nvmm/demo/smallkern/pdir.h +++ b/test/nvmm/demo/smallkern/pdir.h @@ -26,12 +26,16 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#define PAGE_SIZE 4096 -#define PDE_SIZE 8 -#define FRAMESIZE 8 -#define IOM_BEGIN 0x0a0000 /* Start of I/O Memory "hole" */ -#define IOM_END 0x100000 /* End of I/O Memory "hole" */ -#define IOM_SIZE (IOM_END - IOM_BEGIN) +#ifndef PDIR_H_ +#define PDIR_H_ + +#define PAGE_SIZE 4096 +#define PDE_SIZE 8 +#define FRAMESIZE 8 + +#define IOM_BEGIN 0x0a0000 /* Start of I/O Memory "hole" */ +#define IOM_END 0x100000 /* End of I/O Memory "hole" */ +#define IOM_SIZE (IOM_END - IOM_BEGIN) #define SMALLKERNBASE 0x0 #define SMALLKERNTEXTOFF (SMALLKERNBASE + 0x100000) @@ -83,3 +87,5 @@ #define pl2_i(va) (((va) & L2_FRAME) >> L2_SHIFT) #define pl3_i(va) (((va) & L3_FRAME) >> L3_SHIFT) #define pl4_i(va) (((va) & L4_FRAME) >> L4_SHIFT) + +#endif /* !PDIR_H_ */ diff --git a/test/nvmm/demo/smallkern/smallkern.h b/test/nvmm/demo/smallkern/smallkern.h index e393615aba..65de2c5a06 100644 --- a/test/nvmm/demo/smallkern/smallkern.h +++ b/test/nvmm/demo/smallkern/smallkern.h @@ -26,43 +26,62 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#ifndef SMALLKERN_H_ +#define SMALLKERN_H_ + #include #include #include -#include - -#include "pdir.h" #define MM_PROT_READ 0x00 #define MM_PROT_WRITE 0x01 #define MM_PROT_EXECUTE 0x02 -#define ASSERT(a) if (!(a)) fatal("ASSERT"); -#define memset(d, v, l) __builtin_memset(d, v, l) -#define memcpy(d, v, l) __builtin_memcpy(d, v, l) +#define WHITE_ON_BLACK 0x07 +#define RED_ON_BLACK 0x04 +#define GREEN_ON_BLACK 0x02 + +#define ASSERT(a) if (!(a)) fatal("ASSERT"); +#define memset(d, v, l) __builtin_memset(d, v, l) +#define memcpy(d, v, l) __builtin_memcpy(d, v, l) + typedef uint64_t paddr_t; typedef uint64_t vaddr_t; typedef uint64_t pt_entry_t; typedef uint64_t pd_entry_t; typedef uint64_t pte_prot_t; -#define WHITE_ON_BLACK 0x07 -#define RED_ON_BLACK 0x04 -#define GREEN_ON_BLACK 0x02 + +struct smallframe; /* -------------------------------------------------------------------------- */ /* console.c */ -void init_cons(); -void print_ext(int, char *); void print(char *); +void print_banner(void); +void print_ext(int, char *); void print_state(bool, char *); -void print_banner(); /* locore.S */ +extern uint32_t nox_flag; +extern uint64_t *gdt64_start; +extern vaddr_t lapicbase; + +void clts(void); +void cpuid(uint32_t, uint32_t, uint32_t *); +void lcr8(uint64_t); void lidt(void *); -uint64_t rdtsc(); -void jump_kernel(); +void outsb(int port, char *buf, size_t size); +uint64_t rdmsr(uint64_t); +void sti(void); +void vmmcall(void); /* main.c */ void fatal(char *); +void trap(struct smallframe *); + +/* trap.S */ +typedef void (vector)(void); +extern vector *x86_exceptions[]; +extern vector Xintr; /* IDTVEC(intr) */ +#endif /* !SMALLKERN_H_ */ -- 2.41.0