From c2b544368af15be777fc9ae5e83392e2413ecfc2 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 22 May 2019 23:32:44 +0200 Subject: [PATCH] Cpuid and speed *ahem* and cosmetic --- Makefile | 11 ++- include/asm.h | 6 ++ include/ke/cpuid.h | 8 +- kaleid/kernel/init/init.c | 2 +- kaleid/kernel/ke/cpuf.asm | 16 +++- kaleid/kernel/ke/cpuid.c | 35 +++++++- kaleid/kernel/ke/idt.c | 3 +- kaleid/kernel/mm/paging.c | 4 +- kaleid/kernel/sh/musage.c | 183 +++++++++++++++++++++++++++++++------- kaleid/kernel/sh/shcmds.c | 7 +- kaleid/kernel/sh/shell.h | 1 + 11 files changed, 230 insertions(+), 46 deletions(-) diff --git a/Makefile b/Makefile index 3705098..12e21f8 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,8 @@ CWARNS=-Wall -Wextra -Wno-unused-parameter -Wno-implicit-fallthrough -Werror=imp CINCLUDES=-Iinclude CFLAGS1=-nostdlib -ffreestanding -mcmodel=large -std=gnu11 CFLAGS2= -c -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -CFLAGS=$(CFLAGS1) $(CFLAGS2) +CFLAGS= $(CFLAGS1) $(CFLAGS2) +CFLAGS_MATHS= $(CFLAGS1) -c -mno-red-zone -mno-mmx -mno-sse2 ifeq ($(mode), release) CFLAGS += -D_NO_DEBUG @@ -53,7 +54,11 @@ ifeq ($(mode), debug) CFLAGS += -g endif -KCC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CINCLUDES) -D_OSK_SOURCE -D_KALEID_KERNEL -fstack-protector-all +KCC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CINCLUDES) \ + -D_OSK_SOURCE -D_KALEID_KERNEL -fstack-protector-all + +KCC_MATHS=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS_MATHS) $(CINCLUDES) \ + -D_OSK_SOURCE -D_KALEID_KERNEL -fstack-protector-all # Folders MBRDIR=boot/grub @@ -185,7 +190,7 @@ $(KOBJDIR)/kernel/ke/cpuid.o: $(KALEIDDIR)/kernel/ke/cpuid.c \ $(KALEIDDIR)/kernel/ke/cpuf.asm | $(KOBJDIR) @mkdir -p $(shell dirname $@) @$(ASM) $(ASMFLAGS) $(KALEIDDIR)/kernel/ke/cpuf.asm -o $@.1 - @$(KCC) $< -o $@.2 + @$(KCC_MATHS) $< -o $@.2 @$(LD) $(LDFLAGS) -r $@.1 $@.2 -o $@ @rm -f $@.1 $@.2 @echo ${CL2}[$@] ${CL}Compiled.${CL3} diff --git a/include/asm.h b/include/asm.h index aa39caf..4694222 100644 --- a/include/asm.h +++ b/include/asm.h @@ -130,6 +130,12 @@ static inline ushort IoReadDWordFromPort(ushort port) { return ret; } +static inline ulong KeReadStsc(void) { + int eax, edx; + asm volatile ("rdtsc":"=a"(eax),"=d"(edx)); + return ((ulong)edx << 32) + eax; +} + //------------------------------------------// // Misc. I/O // //------------------------------------------// diff --git a/include/ke/cpuid.h b/include/ke/cpuid.h index 50dcd84..f10e53d 100644 --- a/include/ke/cpuid.h +++ b/include/ke/cpuid.h @@ -100,9 +100,9 @@ enum { // Issue a single request to CPUID. Fits 'intel features', for instance // note that even if only "eax" and "edx" are of interest, other registers // will be modified by the operation, so we need to tell the compiler about it. -static inline void CpuCpuid(int code, uint *a, uint *d) +static inline void CpuCpuid(int code, uint *eax, uint *edx) { - asm volatile("cpuid":"=a"(*a),"=d"(*d):"a"(code):"ecx","ebx"); + asm volatile("cpuid":"=a"(*eax),"=d"(*edx):"a"(code):"ecx","ebx"); } // Issue a complete request, storing general registers output as a string @@ -113,7 +113,9 @@ static inline int CpuCpuidString(int code, uint where[4]) return (int)where[0]; } -void CpuGetInfos(void); +void KeGetCpuInfos(void); +double KeGetCpuSpeed(void); +extern void KeActivateSSE(void); // -------------------------------------------------------------------------- // diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 2e4ca66..5fd564e 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -61,7 +61,7 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) // Interrupts launching KeSetupIDT(); KeEnableIRQs(); - CpuGetInfos(); + KeGetCpuInfos(); MmInitGdt(); // Start drivers diff --git a/kaleid/kernel/ke/cpuf.asm b/kaleid/kernel/ke/cpuf.asm index 50d396f..8120a52 100644 --- a/kaleid/kernel/ke/cpuf.asm +++ b/kaleid/kernel/ke/cpuf.asm @@ -25,6 +25,7 @@ [BITS 64] global divideByZero +global KeActivateSSE %include "kaleid/kernel/ke/cpuf.inc" @@ -34,4 +35,17 @@ global divideByZero divideByZero: ret - +;; +;; Activating SSE +;; +KeActivateSSE: + push rax + mov rax, cr0 + and ax, 0xFFFB ;clear coprocessor emulation CR0.EM + or ax, 0x2 ;set coprocessor monitoring CR0.MP + mov cr0, rax + mov rax, cr4 + or ax, 3 << 9 ;set CR4.OSFXSR and CR4.OSXMMEXCPT at the same time + mov cr4, rax + pop rax + ret diff --git a/kaleid/kernel/ke/cpuid.c b/kaleid/kernel/ke/cpuid.c index 3b38f99..90a97af 100644 --- a/kaleid/kernel/ke/cpuid.c +++ b/kaleid/kernel/ke/cpuid.c @@ -25,19 +25,48 @@ #include #include -void CpuGetInfos(void) +void KeGetCpuInfos(void) { uint CpuVendorString[5] = {0}; extern CpuInfo_t CpuInfo; + uint eax = 0; CpuCpuidString(0, CpuVendorString); memmove(CpuInfo.vendorStr, (char *)&CpuVendorString[1], 12*sizeof(char)); - KernLog("\tCPU %s detected\n", CpuInfo.vendorStr); + CpuCpuid(1, &eax, &CpuInfo.featureFlag); + + if (CpuInfo.featureFlag & FEAT_EDX_SSE) { + KeActivateSSE(); + } + + DebugLog("\tCPU %s detected with features %#x and ?? speed %#d Hz ??\n", + CpuInfo.vendorStr, + CpuInfo.featureFlag, + (long)KeGetCpuSpeed() + ); } - +double KeGetCpuSpeed(void) +{ + ulong flags = KePauseIRQs(); + + IoWriteByteOnPort(0x43,0x34); // set PIT channel 0 to single-shot mode + IoWriteByteOnPort(0x40,0); + IoWriteByteOnPort(0x40,0); // program the counter will be + // 0x10000 - n after n ticks + long stsc = KeReadStsc(); + for (int i=0x9000;i>0;i--); + long etsc= KeReadStsc(); + IoWriteByteOnPort(0x43,0x04); + char lo=IoReadByteFromPort(0x40); + char hi=IoReadByteFromPort(0x40); + KeRestoreIRQs(flags); + + ulong ticks = (0x10000 - (hi*256+lo)); + return (etsc-stsc)*1193180.0 / ticks; +} diff --git a/kaleid/kernel/ke/idt.c b/kaleid/kernel/ke/idt.c index 47f23f6..8a1307c 100644 --- a/kaleid/kernel/ke/idt.c +++ b/kaleid/kernel/ke/idt.c @@ -183,7 +183,8 @@ void KeSetupIDT(void) // Load IDT KeLoadIDT(); - DebugLog("\tInterrupt table initialized at %p\n", _KeIdtPtr.base); + //DebugLog("\tInterrupt table initialized at %p\n", _KeIdtPtr.base); + KernLog("\tInterrupts activated\n"); } // diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 5aab6ab..4975b23 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -108,8 +108,8 @@ void MmInitPaging(void) MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE; MmLoadPML4((void *)MmPML4); - DebugLog("\tPaging tables initialized at %p, %p\n", &MmPD, &MmPT); - DebugLog("\tStack Guards at %p, %p\n", MmStackGuards[0], MmStackGuards[1]); + //DebugLog("\tPaging tables initialized at %p, %p\n", &MmPD, &MmPT); + //DebugLog("\tStack Guards at %p, %p\n", MmStackGuards[0], MmStackGuards[1]); } // diff --git a/kaleid/kernel/sh/musage.c b/kaleid/kernel/sh/musage.c index 0219d22..8f67498 100644 --- a/kaleid/kernel/sh/musage.c +++ b/kaleid/kernel/sh/musage.c @@ -54,85 +54,206 @@ error_t CmdMemUsage(int argc, char **argv, char *cmdline) KernLog("Kernel image\n"); - KernLog("\tstarts at:\t\t%p (%4luMB + %4luKB + %4luB)\n", - BtLoaderInfo.kernelAddr, + KernLog("\t%Cstarts at:\t\t%C%p (%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C)\n", + VGA_COLOR_DARK_GREY, + shcol, + (size_t)BtLoaderInfo.kernelAddr, _ADDR_TO_MB((size_t)BtLoaderInfo.kernelAddr), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_KB((size_t)BtLoaderInfo.kernelAddr), - _ADDR_TO_B((size_t)BtLoaderInfo.kernelAddr)); + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_B((size_t)BtLoaderInfo.kernelAddr), + VGA_COLOR_DARK_GREY, + shcol + ); - KernLog("\tends at:\t\t%p (%4luMB + %4luKB + %4luB)\n", + KernLog("\t%Cends at:\t\t%C%p (%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C)\n", + VGA_COLOR_DARK_GREY, + shcol, (size_t)BtLoaderInfo.kernelEndAddr, _ADDR_TO_MB((size_t)BtLoaderInfo.kernelEndAddr), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_KB((size_t)BtLoaderInfo.kernelEndAddr), - _ADDR_TO_B((size_t)BtLoaderInfo.kernelEndAddr)); + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_B((size_t)BtLoaderInfo.kernelEndAddr), + VGA_COLOR_DARK_GREY, + shcol + ); - KernLog("\tsize:\t\t\t%4luMB + %4luKB + %4luB (%p)\n", + KernLog("\t%Csize:\t\t\t%C%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C (%p)\n", + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_MB(img_diff), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_KB(img_diff), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_B(img_diff), - img_diff); + VGA_COLOR_DARK_GREY, + shcol, + img_diff + ); + KernLog("Kernel stack\n"); - KernLog("\tstarts at:\t\t%p (%4luMB + %4luKB + %4luB)\n", - BtLoaderInfo.stackEndAddr, + KernLog("\t%Cstarts at:\t\t%C%p (%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C)\n", + VGA_COLOR_DARK_GREY, + shcol, + (size_t)BtLoaderInfo.stackEndAddr, _ADDR_TO_MB((size_t)BtLoaderInfo.stackEndAddr), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_KB((size_t)BtLoaderInfo.stackEndAddr), - _ADDR_TO_B((size_t)BtLoaderInfo.stackEndAddr)); - - KernLog("\tcurrently at:\t%p (%4luMB + %4luKB + %4luB)\n", + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_B((size_t)BtLoaderInfo.stackEndAddr), + VGA_COLOR_DARK_GREY, + shcol + ); + + KernLog("\t%Ccurrently at:\t%C%p (%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C)\n", + VGA_COLOR_DARK_GREY, + shcol, (size_t)&var, _ADDR_TO_MB((size_t)&var), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_KB((size_t)&var), - _ADDR_TO_B((size_t)&var)); + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_B((size_t)&var), + VGA_COLOR_DARK_GREY, + shcol + ); - KernLog("\tmin address:\t%p (%4luMB + %4luKB + %4luB)\n", + KernLog("\t%Cmin address:\t%C%p (%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C)\n", + VGA_COLOR_DARK_GREY, + shcol, (size_t)BtLoaderInfo.kernelEndAddr+16, _ADDR_TO_MB((size_t)BtLoaderInfo.kernelEndAddr+16), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_KB((size_t)BtLoaderInfo.kernelEndAddr+16), - _ADDR_TO_B((size_t)BtLoaderInfo.kernelEndAddr+16)); + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_B((size_t)BtLoaderInfo.kernelEndAddr+16), + VGA_COLOR_DARK_GREY, + shcol + ); - KernLog("\tsize (cur):\t\t%4luMB + %4luKB + %4luB (%p)\n", + + KernLog("\t%Csize (cur):\t%C%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C (%p)\n", + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_MB(stack_cur), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_KB(stack_cur), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_B(stack_cur), - stack_cur); + VGA_COLOR_DARK_GREY, + shcol, + stack_cur + ); - KernLog("\tsize (max):\t\t%4luMB + %4luKB + %4luB (%p)\n", + KernLog("\t%Csize (max):\t%C%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C (%p)\n", + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_MB(stack_diff), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_KB(stack_diff), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_B(stack_diff), - stack_diff); + VGA_COLOR_DARK_GREY, + shcol, + stack_diff + ); KernLog("Kernel heap\n"); - KernLog("\tstarts at:\t\t%p (%4luMB + %4luKB + %4luB)\n", - heap_start, _ADDR_TO_MB(heap_start), + KernLog("\t%Cstarts at:\t\t%C%p (%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C)\n", + VGA_COLOR_DARK_GREY, + shcol, + heap_start, + _ADDR_TO_MB(heap_start), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_KB(heap_start), - _ADDR_TO_B(heap_start)); + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_B(heap_start), + VGA_COLOR_DARK_GREY, + shcol + ); - KernLog("\tends at:\t\t%p (%4luMB + %4luKB + %4luB)\n", - heap_end, _ADDR_TO_MB(heap_end), + KernLog("\t%Cends at:\t\t%C%p (%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C)\n", + VGA_COLOR_DARK_GREY, + shcol, + heap_end, + _ADDR_TO_MB(heap_end), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_KB(heap_end), - _ADDR_TO_B(heap_end)); + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_B(heap_end), + VGA_COLOR_DARK_GREY, + shcol + ); - KernLog("\tmax addr:\t\t%p (%4luMB + %4luKB + %4luB)\n", + KernLog("\t%Cmax addr:\t\t%C%p (%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C)\n", + VGA_COLOR_DARK_GREY, + shcol, heap_start + heap_max, _ADDR_TO_MB(heap_start + heap_max), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_KB(heap_start + heap_max), - _ADDR_TO_B(heap_start + heap_max)); + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_B(heap_start + heap_max), + VGA_COLOR_DARK_GREY, + shcol + ); - KernLog("\tsize (cur):\t\t%4luMB + %4luKB + %4luB (%p)\n", + KernLog("\t%Csize (cur):\t%C%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C (%p)\n", + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_MB(heap_diff), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_KB(heap_diff), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_B(heap_diff), - heap_diff); + VGA_COLOR_DARK_GREY, + shcol, + heap_diff + ); - KernLog("\tsize (max):\t\t%4luMB + %4luKB + %4luB (%p)\n", + KernLog("\t%Csize (max):\t%C%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C (%p)\n", + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_MB(heap_max), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_KB(heap_max), + VGA_COLOR_DARK_GREY, + shcol, _ADDR_TO_B(heap_max), - heap_max); + VGA_COLOR_DARK_GREY, + shcol, + heap_max + ); return EOK; } diff --git a/kaleid/kernel/sh/shcmds.c b/kaleid/kernel/sh/shcmds.c index d7fa41c..26a8666 100644 --- a/kaleid/kernel/sh/shcmds.c +++ b/kaleid/kernel/sh/shcmds.c @@ -97,6 +97,11 @@ error_t CmdColor(int argc, char **argv, char *cmdline) return EOK; } +error_t CmdCpuid(int argc, char **argv, char *cmdline) +{ + return EOK; +} + error_t CmdDate(int argc, char **argv, char *cmdline) { KernLog("%.10s\n", KeFormatCurTime()); @@ -228,13 +233,13 @@ error_t CmdVersion(int argc, char **argv, char *cmdline) } //----------------------------------------------------------------------------// - Command_t cmdtable[] = { { "args", CmdArgs, "Print command line" }, { "beep", CmdBeep, "Make a beep" }, { "cls", CmdClear, "Clears standard output" }, { "color", CmdColor, "Change shell text color" }, + { "cpuid", CmdCpuid, "Request a cpuid info." }, { "date", CmdDate, "Print date" }, { "die", CmdDie, "Die painfully" }, { "help", CmdHelp, "Show this message" }, diff --git a/kaleid/kernel/sh/shell.h b/kaleid/kernel/sh/shell.h index 0ce9e23..215855c 100644 --- a/kaleid/kernel/sh/shell.h +++ b/kaleid/kernel/sh/shell.h @@ -34,6 +34,7 @@ #include #include #include +#include void IoScrollDown(void); void IoScrollUp(void);