From dc1cfa61510b6f4c6ad28c0cfea2c9396270acfb Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Fri, 5 Apr 2019 10:20:10 +0200 Subject: [PATCH] Global variable (BootTab/CPU) stuff --- kaleid/include/kernel/base.h | 31 ++++++----- kaleid/include/kernel/mm.h | 4 +- kaleid/include/kernel/panic.h | 2 +- kaleid/include/kernel/proc.h | 4 +- kaleid/include/kernel/sched.h | 8 +-- kaleid/kernel/buf/bprint.c | 13 +++-- kaleid/kernel/init/init.c | 98 +++++++++++++++++------------------ kaleid/kernel/init/table.c | 11 ++-- kaleid/kernel/io/vga.c | 10 ++-- kaleid/kernel/ke/panic.c | 4 +- kaleid/kernel/mm/map.c | 14 ++--- kaleid/kernel/ps/sched.c | 12 ++--- 12 files changed, 107 insertions(+), 104 deletions(-) diff --git a/kaleid/include/kernel/base.h b/kaleid/include/kernel/base.h index 42622ee..3451936 100644 --- a/kaleid/include/kernel/base.h +++ b/kaleid/include/kernel/base.h @@ -60,10 +60,7 @@ typedef enum TermColor_t TermColor_t; #define _KeCurCPU 0 // Process_t structure of current CPU -#define KeCurCPU (cpuTable[_KeCurCPU]) - -// Access the BootInfo_t structure -#define BtGetBootInfo(x) (bootTab.x) +//#define KeCurCPU (cpuTable[_KeCurCPU]) //------------------------------------------// @@ -77,10 +74,13 @@ struct Processor_t // CPU APIC id int apicId; + // CPU Vendor String (always 12 characters) char vendorStr[12]; + // CPU Model code (enum in cpu.h) int modelCode; + // CPU Features flag uint featureFlag; @@ -176,19 +176,24 @@ struct BootInfo_t //------------------------------------------// -extern volatile int cpuCount; -extern volatile BootInfo_t bootTab; -extern volatile Processor_t cpuTable[NCPUS]; +extern int KeCPUCount; +extern Processor_t _KeCPUTable[NCPUS]; + +extern volatile Processor_t *KeCurCPU; +extern volatile BootInfo_t BtBootTab; //------------------------------------------// -#define DEC_PER_CPU(pref, name, field, type) \ - static inline type pref##Get##name() { return KeGetCurCPU().field; } \ - static inline void _##pref##Set##name(type __val) \ - { KeGetCurCPU().field = __val; } +#define DEC_PER_CPU(pref, name, field, type) \ + static inline type pref##Get##name() { return KeGetCurCPU()->field; } \ + static inline void _##pref##Set##name(type __val) \ + { KeGetCurCPU()->field = __val; } -#define BARRIER() do{\ - asm volatile("": : :"memory");__sync_synchronize();}while(0) +#define BARRIER() \ + do { \ + asm volatile("": : :"memory"); \ + __sync_synchronize(); \ + } while(0) //------------------------------------------// diff --git a/kaleid/include/kernel/mm.h b/kaleid/include/kernel/mm.h index c06bd8d..8ee32b1 100644 --- a/kaleid/include/kernel/mm.h +++ b/kaleid/include/kernel/mm.h @@ -66,7 +66,7 @@ struct GdtEntry_t uchar access; // determine what ring this segment can be used in uchar granularity; uchar highBase; // last 8 bits -} __attribute__((packed)); +} __attribute__((__packed__)); // The gdt pointer struct GdtPtr_t @@ -74,7 +74,7 @@ struct GdtPtr_t uchar limit; // upper 16 bits ushort base; // address of the first entry } - __attribute__((packed)); + __attribute__((__packed__)); // -------------------------------------------------------------------------- // diff --git a/kaleid/include/kernel/panic.h b/kaleid/include/kernel/panic.h index 5d97862..d24a16d 100644 --- a/kaleid/include/kernel/panic.h +++ b/kaleid/include/kernel/panic.h @@ -32,7 +32,7 @@ //------------------------------------------// #define PANICSTR_SIZE 1024 -extern volatile char KePanicStr[PANICSTR_SIZE]; +extern volatile bool KeIsPanicking; //------------------------------------------// diff --git a/kaleid/include/kernel/proc.h b/kaleid/include/kernel/proc.h index 2e4e962..6c9f373 100644 --- a/kaleid/include/kernel/proc.h +++ b/kaleid/include/kernel/proc.h @@ -77,8 +77,8 @@ struct Process_t //------------------------------------------// -#define PsCurProc (KeCurCPU.process) -#define PsCurThread (KeCurCPU.thread) +#define PsCurProc (KeCurCPU->process) +#define PsCurThread (KeCurCPU->thread) //DEC_PER_CPU(Ps, CurProc, process, Process_t *); //DEC_PER_CPU(Ps, CurThread, thread, Thread_t *); diff --git a/kaleid/include/kernel/sched.h b/kaleid/include/kernel/sched.h index 76bd799..9c3ae35 100644 --- a/kaleid/include/kernel/sched.h +++ b/kaleid/include/kernel/sched.h @@ -61,10 +61,10 @@ extern const char *PsPrioClassesNames[]; // Re-scheduling and preemption // XXX atomic operations // -#define PsRequestReSched() (++KeCurCPU.needReSched) -#define PsDisablePreemption() (++KeCurCPU.preemptCount) -#define PsEnablePreemption() do { assert(KeCurCPU.preemptCount > 0); \ - --KeCurCPU.preemptCount; } while(0) +#define PsRequestReSched() (++KeCurCPU->needReSched) +#define PsDisablePreemption() (++KeCurCPU->preemptCount) +#define PsEnablePreemption() do { assert(KeCurCPU->preemptCount > 0); \ + --KeCurCPU->preemptCount; } while(0) //------------------------------------------// diff --git a/kaleid/kernel/buf/bprint.c b/kaleid/kernel/buf/bprint.c index 2118a5f..099a968 100644 --- a/kaleid/kernel/buf/bprint.c +++ b/kaleid/kernel/buf/bprint.c @@ -24,7 +24,7 @@ #include -extern error_t bputc(Buffer_t *buf, uchar ch); +error_t bputc(Buffer_t *buf, uchar ch); error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap); // @@ -123,7 +123,7 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap) assert(buf && buf->initDone == INITOK); - if (!buf) return EINVAL; + if (!buf || !fmt) return EINVAL; if (buf->state != BS_RDWR && buf->state != BS_WRONLY) { return EBADF; } @@ -241,7 +241,6 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap) // // The type field // - type = *fmt++; // Characters @@ -300,10 +299,10 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap) s = convbuf; // FIXME: this works, but is ugly as hell - #define bdoconvrt(pref, type, vtype) \ - do { \ - type i_##type = (type)va_arg(ap, vtype); \ - pref##toa(i_##type, s, base); \ + #define bdoconvrt(pref, type, vtype) \ + do { \ + type i_##type = (type)va_arg(ap, vtype); \ + pref##toa(i_##type, s, base); \ } while (0) if (!l) { diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index d0b2eea..9e1bd00 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -44,69 +44,69 @@ void BtInitBootInfo(multiboot_info_t *mbi) KalAlwaysAssert(mbi); //Retrieves the bootloader flags to ensure infos are valid - BtGetBootInfo(btldr).grubFlags = mbi->flags; + BtBootTab.btldr.grubFlags = mbi->flags; - if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_BOOT_LOADER_NAME) { - BtGetBootInfo(btldr).grubName = (char*)(ulong)(mbi->boot_loader_name); - BtGetBootInfo(btldr).kernelAddr = (void*)&MB_header; - BtGetBootInfo(btldr).kernelEndAddr = (void*)newKernelEnd; - BtGetBootInfo(btldr).valid = 1; + if (BtBootTab.btldr.grubFlags & MULTIBOOT_INFO_BOOT_LOADER_NAME) { + BtBootTab.btldr.grubName = (char*)(ulong)(mbi->boot_loader_name); + BtBootTab.btldr.kernelAddr = (void*)&MB_header; + BtBootTab.btldr.kernelEndAddr = (void*)newKernelEnd; + BtBootTab.btldr.valid = 1; } - if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_MODS) { - BtGetBootInfo(btldr).modulesCount = mbi->mods_count; - BtGetBootInfo(btldr).modulesAddr = (void*)(ulong)mbi->mods_addr; + if (BtBootTab.btldr.grubFlags & MULTIBOOT_INFO_MODS) { + BtBootTab.btldr.modulesCount = mbi->mods_count; + BtBootTab.btldr.modulesAddr = (void*)(ulong)mbi->mods_addr; } //Retrieves the drives informations - if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_DRIVE_INFO) { - BtGetBootInfo(drives).bufferLength = mbi->drives_length; - BtGetBootInfo(drives).bufferAddr = (void*)(ulong)mbi->drives_addr; - BtGetBootInfo(drives).bufferValid = 1; + if (BtBootTab.btldr.grubFlags & MULTIBOOT_INFO_DRIVE_INFO) { + BtBootTab.drives.bufferLength = mbi->drives_length; + BtBootTab.drives.bufferAddr = (void*)(ulong)mbi->drives_addr; + BtBootTab.drives.bufferValid = 1; } - if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_BOOTDEV) { - BtGetBootInfo(drives).bootDrv = mbi->boot_device; - BtGetBootInfo(drives).drvValid = 1; + if (BtBootTab.btldr.grubFlags & MULTIBOOT_INFO_BOOTDEV) { + BtBootTab.drives.bootDrv = mbi->boot_device; + BtBootTab.drives.drvValid = 1; } //Retrieves the memory informations - if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_MEMORY) { - BtGetBootInfo(memory).lowMemory = mbi->mem_lower; - BtGetBootInfo(memory).upMemory = mbi->mem_upper; - BtGetBootInfo(memory).memValid = 1; + if (BtBootTab.btldr.grubFlags & MULTIBOOT_INFO_MEMORY) { + BtBootTab.memory.lowMemory = mbi->mem_lower; + BtBootTab.memory.upMemory = mbi->mem_upper; + BtBootTab.memory.memValid = 1; } - if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_MEM_MAP) { - BtGetBootInfo(memory).mapAddr = (void*)(ulong)mbi->mmap_addr; - BtGetBootInfo(memory).mapLength = mbi->mmap_length; - BtGetBootInfo(memory).mapValid = 1; + if (BtBootTab.btldr.grubFlags & MULTIBOOT_INFO_MEM_MAP) { + BtBootTab.memory.mapAddr = (void*)(ulong)mbi->mmap_addr; + BtBootTab.memory.mapLength = mbi->mmap_length; + BtBootTab.memory.mapValid = 1; } // Retrieves video mode informations - if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_VBE_INFO) { - BtGetBootInfo(video).vbeControl = (void*)(ulong)mbi->vbe_control_info; - BtGetBootInfo(video).vbeModeInfo = (void*)(ulong)mbi->vbe_mode_info; - BtGetBootInfo(video).vbeMode = mbi->vbe_mode; - BtGetBootInfo(video).vbeInterfaceSeg = mbi->vbe_interface_seg; - BtGetBootInfo(video).vbeInterfaceOff = mbi->vbe_interface_off; - BtGetBootInfo(video).vbeInterfaceLen = mbi->vbe_interface_len; - BtGetBootInfo(video).vbeValid = 1; + if (BtBootTab.btldr.grubFlags & MULTIBOOT_INFO_VBE_INFO) { + BtBootTab.video.vbeControl = (void*)(ulong)mbi->vbe_control_info; + BtBootTab.video.vbeModeInfo = (void*)(ulong)mbi->vbe_mode_info; + BtBootTab.video.vbeMode = mbi->vbe_mode; + BtBootTab.video.vbeInterfaceSeg = mbi->vbe_interface_seg; + BtBootTab.video.vbeInterfaceOff = mbi->vbe_interface_off; + BtBootTab.video.vbeInterfaceLen = mbi->vbe_interface_len; + BtBootTab.video.vbeValid = 1; } - if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_FRAMEBUFFER_INFO) { - BtGetBootInfo(video).framebufferAddr = (void*)mbi->framebuffer_addr; - BtGetBootInfo(video).framebufferPitch = mbi->framebuffer_pitch; - BtGetBootInfo(video).framebufferWidth = mbi->framebuffer_width; - BtGetBootInfo(video).framebufferHeight= mbi->framebuffer_height; - BtGetBootInfo(video).framebufferBpp = mbi->framebuffer_bpp; - BtGetBootInfo(video).framebufferType = mbi->framebuffer_type; - BtGetBootInfo(video).fbuValid = 1; + if (BtBootTab.btldr.grubFlags & MULTIBOOT_INFO_FRAMEBUFFER_INFO) { + BtBootTab.video.framebufferAddr = (void*)mbi->framebuffer_addr; + BtBootTab.video.framebufferPitch = mbi->framebuffer_pitch; + BtBootTab.video.framebufferWidth = mbi->framebuffer_width; + BtBootTab.video.framebufferHeight= mbi->framebuffer_height; + BtBootTab.video.framebufferBpp = mbi->framebuffer_bpp; + BtBootTab.video.framebufferType = mbi->framebuffer_type; + BtBootTab.video.fbuValid = 1; } // Retrieves the firmware infos - if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_CONFIG_TABLE) { - BtGetBootInfo(firmware).romTable = mbi->config_table; - BtGetBootInfo(firmware).romValid = 1; + if (BtBootTab.btldr.grubFlags & MULTIBOOT_INFO_CONFIG_TABLE) { + BtBootTab.firmware.romTable = mbi->config_table; + BtBootTab.firmware.romValid = 1; } - if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_APM_TABLE) { - BtGetBootInfo(firmware).apmTable = mbi->apm_table; - BtGetBootInfo(firmware).apmValid = 1; + if (BtBootTab.btldr.grubFlags & MULTIBOOT_INFO_APM_TABLE) { + BtBootTab.firmware.apmTable = mbi->apm_table; + BtBootTab.firmware.apmValid = 1; } } @@ -118,11 +118,11 @@ void BtInitSanity(uint mbMagic){ if (!(mbMagic == MULTIBOOT_BOOTLOADER_MAGIC)) KeStartPanic("[Init] Magic number %x is incorrect\n", mbMagic); - tmp = (BtGetBootInfo(btldr).kernelEndAddr - - BtGetBootInfo(btldr).kernelAddr) / KB; + tmp = (BtBootTab.btldr.kernelEndAddr + - BtBootTab.btldr.kernelAddr) / KB; DebugLog("[Init] Kernel successfully loaded at %p with %x magic\n" " and it uses %d Kio\n\n", - BtGetBootInfo(btldr).kernelAddr, + BtBootTab.btldr.kernelAddr, mbMagic,tmp); } diff --git a/kaleid/kernel/init/table.c b/kaleid/kernel/init/table.c index 2878b61..66db3e3 100644 --- a/kaleid/kernel/init/table.c +++ b/kaleid/kernel/init/table.c @@ -24,11 +24,10 @@ #include -volatile int cpuCount = 1; -volatile Processor_t cpuTable[NCPUS] = {0}; +int KeCPUCount = 1; +Processor_t _KeCPUTable[NCPUS] = {0}; -volatile BootInfo_t bootTab = {0}; -Terminal_t *StdOut = 0, *StdDbg = 0; - -volatile char KePanicStr[PANICSTR_SIZE] = {0}; +volatile BootInfo_t BtBootTab = {0}; +volatile bool KeIsPanicking = 0; +volatile Processor_t *KeCurCPU = &_KeCPUTable[0]; diff --git a/kaleid/kernel/io/vga.c b/kaleid/kernel/io/vga.c index 0230143..c5bcd92 100644 --- a/kaleid/kernel/io/vga.c +++ b/kaleid/kernel/io/vga.c @@ -41,7 +41,7 @@ // error_t bvgaflusher(Buffer_t *buf) { - ushort *fbp = BtGetBootInfo(video).framebufferAddr; + ushort *fbp = BtBootTab.video.framebufferAddr; const uchar color = 0xf; uchar *currentLine = buf->wp - buf->lineLen - buf->lastLF; @@ -54,8 +54,8 @@ error_t bvgaflusher(Buffer_t *buf) } const size_t bufSize = buf->nLines * buf->lineLen; - ushort *fbe = BtGetBootInfo(video).framebufferAddr - + (bufSize * sizeof(ushort)); + ushort *fbe = BtBootTab.video.framebufferAddr + + (bufSize * sizeof(ushort)); if (fbp < fbe) { const ushort filler = VGA_ComputeEntry(' ', color); @@ -73,8 +73,8 @@ error_t IoInitVGABuffer(void) static char bvgabuffer[1 * MB]; BStdOut = BOpenLineBuf(bvgabuffer, BS_WRONLY, - BtGetBootInfo(video).framebufferWidth, - BtGetBootInfo(video).framebufferHeight, + BtBootTab.video.framebufferWidth, + BtBootTab.video.framebufferHeight, 8, bvgaflusher); BStdDbg = BStdOut; diff --git a/kaleid/kernel/ke/panic.c b/kaleid/kernel/ke/panic.c index 7e153a5..04a1ae9 100644 --- a/kaleid/kernel/ke/panic.c +++ b/kaleid/kernel/ke/panic.c @@ -62,12 +62,12 @@ noreturn void KeStartPanic(const char *fmt, ...) fmt = "(no message given)"; } - if (KePanicStr[0] != 0) { + if (KeIsPanicking) { vbprintf(BStdOut, "\nDouble panic!", NULL); KeHaltCPU(); } - KePanicStr[0] = 1; + KeIsPanicking = 1; // We don't check vbprintf's output // If it fails, what could we do anyway? diff --git a/kaleid/kernel/mm/map.c b/kaleid/kernel/mm/map.c index 3d39401..9c96699 100644 --- a/kaleid/kernel/mm/map.c +++ b/kaleid/kernel/mm/map.c @@ -52,19 +52,19 @@ static error_t InitMemoryMap(void) uint i = 0; // sanity checks - if (!BtGetBootInfo(memory).memValid && BtGetBootInfo(memory).mapValid) + if (!BtBootTab.memory.memValid && BtBootTab.memory.mapValid) return ENXIO; - if ((BtGetBootInfo(memory).upMemory / (MB/KB)) <= MINIMUM_RAM_SIZE) + if ((BtBootTab.memory.upMemory / (MB/KB)) <= MINIMUM_RAM_SIZE) return ENOMEM; // Ok then we can work ------------------------------------------------------ // // the memory map provided by GRUB via the BIOS - currentEntry = (multiboot_memory_map_t*)BtGetBootInfo(memory).mapAddr; + currentEntry = (multiboot_memory_map_t*)BtBootTab.memory.mapAddr; // End address of the map mapEnd = (multiboot_memory_map_t*) - ((ulong)currentEntry + (ulong)BtGetBootInfo(memory).mapLength); + ((ulong)currentEntry + (ulong)BtBootTab.memory.mapLength); // fill the map while (currentEntry < mapEnd) { @@ -112,7 +112,7 @@ size_t MmGetAvailZoneSize(void *start) { uint i; // Because the kernel is the kernel - if (start < BtGetBootInfo(btldr).kernelEndAddr) + if (start < BtBootTab.btldr.kernelEndAddr) return 0; // Search the zone where the start address is @@ -137,8 +137,8 @@ void *MmGetFirstAvailZone(void *start) { void *current = 0; // Because the kernel is the kernel - if ((ulong)start < (ulong)BtGetBootInfo(btldr).kernelEndAddr) { - return MmGetFirstAvailZone(BtGetBootInfo(btldr).kernelEndAddr); + if ((ulong)start < (ulong)BtBootTab.btldr.kernelEndAddr) { + return MmGetFirstAvailZone(BtBootTab.btldr.kernelEndAddr); } // Search the zone where the start address is diff --git a/kaleid/kernel/ps/sched.c b/kaleid/kernel/ps/sched.c index 09794bc..31c9781 100644 --- a/kaleid/kernel/ps/sched.c +++ b/kaleid/kernel/ps/sched.c @@ -46,13 +46,13 @@ Process_t procs[] = { //------------------------------------------// -#define ReSchedFlag (KeCurCPU.needReSched) -#define PreemptCount (KeCurCPU.preemptCount) +#define ReSchedFlag (KeCurCPU->needReSched) +#define PreemptCount (KeCurCPU->preemptCount) -#define IdlePrioProcs (KeCurCPU.idlePrioProcs) -#define ReglPrioProcs (KeCurCPU.reglPrioProcs) -#define ServPrioProcs (KeCurCPU.servPrioProcs) -#define TimeCritProcs (KeCurCPU.timeCritProcs) +#define IdlePrioProcs (KeCurCPU->idlePrioProcs) +#define ReglPrioProcs (KeCurCPU->reglPrioProcs) +#define ServPrioProcs (KeCurCPU->servPrioProcs) +#define TimeCritProcs (KeCurCPU->timeCritProcs) //------------------------------------------//