CPU Speed is now detected successfully

This commit is contained in:
Adrien Bourmault 2019-11-15 00:09:20 +01:00
parent b016271c24
commit 17ccd2d53b
8 changed files with 66 additions and 42 deletions

View File

@ -264,11 +264,16 @@ OS/K: $(dep) ./ProjectTree $(BINDIR)/kaleid
## QEMU/DEBUG RELATED ## QEMU/DEBUG RELATED
test: all install testkvm: all install
@qemu-system-x86_64 -vga std -cpu core2duo -enable-kvm -soundhw pcspk -s \ @qemu-system-x86_64 -vga std -cpu core2duo -enable-kvm -soundhw pcspk -s \
-rtc base=localtime -m $(ram) -hda $(installdisk) \ -rtc base=localtime -m $(ram) -hda $(installdisk) \
-d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & -d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log &
test: all install
@qemu-system-x86_64 -vga std -cpu core2duo -soundhw pcspk -s \
-rtc base=localtime -m $(ram) -hda $(installdisk) \
-d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log &
test32: all install test32: all install
@qemu-system-i386 -m $(ram) -hda $(installdisk) -d \ @qemu-system-i386 -m $(ram) -hda $(installdisk) -d \
cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log &

View File

@ -113,8 +113,17 @@ static inline int CpuCpuidString(int code, uint where[4])
return (int)where[0]; return (int)where[0];
} }
// Read the TSC register
static inline ulong CpuRdtsc(void)
{
uint lo, hi;
asm volatile ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (ulong)lo)|( ((ulong)hi)<<32 );
}
void KeGetCpuInfos(void); void KeGetCpuInfos(void);
double KeGetCpuSpeed(void); double KeGetCpuFrequency(void);
void KeActivateSSE(void); void KeActivateSSE(void);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //

View File

@ -43,7 +43,7 @@ struct Time_t
uchar century; uchar century;
} __attribute__((packed)); } __attribute__((packed));
struct TimerFilo_t { struct Timer_t {
uint countDown; uint countDown;
ulong sema; ulong sema;
} __attribute__((packed)); } __attribute__((packed));
@ -62,6 +62,7 @@ void KeSetCurTime(Time_t);
void KeEnablePIT(void); void KeEnablePIT(void);
void KeSleep(uint); void KeSleep(uint);
Timer_t *KeSetTimer(uint delay);
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//

View File

@ -37,7 +37,7 @@ typedef Spinlock_t Lock_t;
//typedef struct Lock_t Lock_t; //typedef struct Lock_t Lock_t;
typedef struct Time_t Time_t; typedef struct Time_t Time_t;
typedef struct TimerFilo_t TimerFilo_t; typedef struct Timer_t Timer_t;
typedef struct Buffer_t Buffer_t; typedef struct Buffer_t Buffer_t;
typedef struct ListHead_t ListHead_t; typedef struct ListHead_t ListHead_t;
typedef struct ListNode_t ListNode_t; typedef struct ListNode_t ListNode_t;
@ -108,6 +108,9 @@ struct CpuInfo_t
// CPU Features flag // CPU Features flag
uint featureFlag; uint featureFlag;
// CPU Frequency (Hz)
double frequency;
}; };
struct ISRFrame_t { struct ISRFrame_t {

View File

@ -65,21 +65,23 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg)
MmInitPaging(); MmInitPaging();
MmInitHeap(); MmInitHeap();
// Interrupts launching // Basics for interrupts
KeSetupIDT(); KeSetupIDT();
KeEnableIRQs(); KeEnableIRQs();
MmInitGdt();
// Clocks
KeEnableRTC(); KeEnableRTC();
KeEnablePIT(); KeEnablePIT();
// Start drivers
KeGetCpuInfos(); KeGetCpuInfos();
IoEnableKeyb();
// Memory (2)
MmInitGdt();
MmActivatePageHandler(); MmActivatePageHandler();
// Drivers
IoEnableKeyb();
// Command line (kernel mode)
ShStartShell(); ShStartShell();
// Exit !
PoShutdown(); PoShutdown();
} }

View File

@ -23,6 +23,7 @@
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include <ke/cpuid.h> #include <ke/cpuid.h>
#include <ke/time.h>
#include <io/vga.h> #include <io/vga.h>
void KeGetCpuInfos(void) void KeGetCpuInfos(void)
@ -41,32 +42,22 @@ void KeGetCpuInfos(void)
KeActivateSSE(); KeActivateSSE();
} }
DebugLog("\tCPU %s detected with features %#x and ?? speed %#d Hz ??\n", CpuInfo.frequency = KeGetCpuFrequency();
DebugLog("\tCPU %s %#d KHz detected with features %#x\n",
CpuInfo.vendorStr, CpuInfo.vendorStr,
CpuInfo.featureFlag, (long)(CpuInfo.frequency / 1000.0),
(long)KeGetCpuSpeed() CpuInfo.featureFlag
); );
} }
double KeGetCpuFrequency(void)
double KeGetCpuSpeed(void)
{ {
ulong flags = KePauseIRQs(); ulong first = CpuRdtsc();
KeSleep(300);
ulong second = CpuRdtsc();
IoWriteByteOnPort(0x43,0x34); // set PIT channel 0 to single-shot mode return ((double)(second) - (double)(first)) / 300.0;
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;
} }

View File

@ -29,7 +29,7 @@
#define COUNTDONE 1 #define COUNTDONE 1
#define PIT_FREQUENCY 1000 // Hz = 10ms #define PIT_FREQUENCY 1000 // Hz = 10ms
static TimerFilo_t timerFilo[20]; //20 concurrent sleep max static Timer_t Timer[20]; //20 concurrent sleep max
static ulong Ticks = 0; static ulong Ticks = 0;
static Time_t CurTime; static Time_t CurTime;
static char TimeFmtBuf[22] = { 0 }; static char TimeFmtBuf[22] = { 0 };
@ -42,22 +42,22 @@ static void HandlePIT(ISRFrame_t *regs)
Ticks++; Ticks++;
for (uchar i = 0; i < 20; i++) { for (uchar i = 0; i < 20; i++) {
if (timerFilo[i].countDown > 0) { if (Timer[i].countDown > 0) {
timerFilo[i].countDown--; Timer[i].countDown--;
if (timerFilo[i].countDown == 0) if (Timer[i].countDown == 0)
timerFilo[i].sema = 1; Timer[i].sema = 1;
} }
KeSendEOItoPIC(0x28); KeSendEOItoPIC(0x28);
} }
} }
static TimerFilo_t* KeFindTimerBlock(void) static Timer_t* KeFindTimerBlock(void)
{ {
for(uchar i = 0; i < 20; i++) { for(uchar i = 0; i < 20; i++) {
if (timerFilo[i].countDown == 0) if (Timer[i].countDown == 0)
return &timerFilo[i]; return &Timer[i];
} }
return NULL; return NULL;
@ -65,9 +65,9 @@ static TimerFilo_t* KeFindTimerBlock(void)
void KeSleep(uint delay) void KeSleep(uint delay)
{ {
struct TimerFilo_t *timerBlock; struct Timer_t *timerBlock;
if ((timerBlock = (TimerFilo_t*)KeFindTimerBlock()) == NULL) if ((timerBlock = (Timer_t*)KeFindTimerBlock()) == NULL)
return; return;
timerBlock->countDown = delay; timerBlock->countDown = delay;
@ -78,6 +78,20 @@ void KeSleep(uint delay)
timerBlock->sema = 0; timerBlock->sema = 0;
} }
Timer_t *KeSetTimer(uint delay)
{
struct Timer_t *timerBlock;
if ((timerBlock = (Timer_t*)KeFindTimerBlock()) == NULL)
return NULL;
timerBlock->countDown = delay;
DebugLog("Timer set %d ms", delay);
return timerBlock;
}
void KeEnablePIT(void) void KeEnablePIT(void)
{ {
ulong flags = KePauseIRQs(); ulong flags = KePauseIRQs();

View File

@ -30,7 +30,6 @@ static ulong Ticks = 0;
static Time_t OriginTime; static Time_t OriginTime;
// TODO asnprintf() // TODO asnprintf()
static char TimeFmtBuf[22] = { 0 };
static uchar RTC_RATE = 0x05; //2048Hz static uchar RTC_RATE = 0x05; //2048Hz
static char time24or12Mode; static char time24or12Mode;