Refactoring and improvements

This commit is contained in:
Ali Mashtizadeh 2014-07-13 23:16:54 -07:00
parent eb6efaa773
commit 27c65e8992
11 changed files with 104 additions and 26 deletions

View File

@ -11,6 +11,7 @@ src_amd64 = [
"amd64/multiboot.S",
"amd64/mbentry.c",
# AMD64
"amd64/critical.c",
"amd64/debug.c",
"amd64/disasm.c",
"amd64/trap.c",

40
sys/amd64/critical.c Normal file
View File

@ -0,0 +1,40 @@
#include <stdint.h>
#include <kassert.h>
#include <kconfig.h>
#include <mp.h>
#include <spinlock.h>
#include "amd64.h"
#include "amd64op.h"
uint32_t lockLevel[MAX_CPUS];
void
Critical_Init()
{
int c;
for (c = 0; c < MAX_CPUS; c++)
{
lockLevel[c] = 0;
}
}
void
Critical_Enter()
{
disable_interrupts();
lockLevel[CPU()]++;
}
void
Critical_Exit()
{
lockLevel[CPU()]--;
if (lockLevel[CPU()] == 0)
{
enable_interrupts();
}
}

View File

@ -2,6 +2,7 @@
* LAPIC
*/
#include <stdbool.h>
#include <stdint.h>
#include <kassert.h>
@ -40,6 +41,8 @@
#define LAPIC_TDCR 0x03E0 /* Time Divide Configuration Register */
#define LAPIC_TDCR_X1 0x000B /* Divide counts by 1 */
bool lapicInitialized = false;
static uint32_t *
LAPIC_GetBase()
{
@ -68,7 +71,10 @@ LAPIC_Write(uint16_t reg, uint32_t val)
uint32_t
LAPIC_CPU()
{
return LAPIC_Read(LAPIC_ID) >> 24;
if (lapicInitialized)
return LAPIC_Read(LAPIC_ID) >> 24;
else
return 0;
}
void
@ -99,6 +105,8 @@ LAPIC_Init()
base = rdmsr(IA32_APIC_BASE_MSR);
wrmsr(IA32_APIC_BASE_MSR, base | IA32_APIC_BASE_MSR_ENABLE);
lapicInitialized = true;
kprintf("LAPIC: CPU %d found at 0x%016llx\n", LAPIC_CPU(), base);
// Enable interrupts

View File

@ -85,6 +85,14 @@ void Machine_SyscallInit()
kprintf("Done!\n");
}
void Machine_EarlyInit()
{
Critical_Init();
Critical_Enter();
Console_Init();
PAlloc_Init();
}
void Machine_Init()
{
Machine_GDTInit();
@ -102,6 +110,7 @@ void Machine_Init()
PCI_Init();
IDE_Init();
Critical_Exit();
breakpoint();
}

View File

@ -12,7 +12,6 @@
#include "amd64.h"
#include "multiboot.h"
void mbentry(unsigned long magic, unsigned long addr);
#define CHECK_FLAG(flag, bit) ((flag) & (1 << (bit)))
@ -20,8 +19,8 @@ void mbentry(unsigned long magic, unsigned long addr);
#define PAGE_ALIGN __attribute__((aligned(PGSIZE)))
#define DATA_SECTION __attribute__((section(".data")))
extern void Machine_EarlyInit();
extern void Machine_Init();
extern void PAlloc_Init();
extern void PAlloc_AddRegion(uintptr_t start, uintptr_t len);
PAGE_ALIGN DATA_SECTION PageTable bootpgtbl3 = { .entries = {
@ -59,8 +58,7 @@ mb_entry(unsigned long magic, unsigned long addr)
{
multiboot_info_t *mbi = (multiboot_info_t *)addr;
Console_Init();
PAlloc_Init();
Machine_EarlyInit();
/* @r{Am I booted by a Multiboot-compliant boot loader?} */
if (magic != MULTIBOOT_BOOTLOADER_MAGIC)

View File

@ -14,6 +14,7 @@ extern void trap_pop(TrapFrame *tf);
static InteruptGate64 idt[256];
static PseudoDescriptor idtdesc;
static uint64_t intStats[256];
void
Trap_Init()
@ -52,6 +53,11 @@ Trap_Init()
lidt(&idtdesc);
// Zero out interrupt stats
for (i = 0; i < 256; i++) {
intStats[i] = 0;
}
kprintf("Done!\n");
}
@ -87,6 +93,9 @@ Trap_Dump(TrapFrame *tf)
void
trap_entry(TrapFrame *tf)
{
// XXX: ATOMIC!
intStats[tf->vector]++;
// Kernel Debugger
if (tf->vector == T_BP && tf->cs == SEL_KCS)
{
@ -120,7 +129,7 @@ trap_entry(TrapFrame *tf)
if (tf->vector >= T_IRQ_BASE && tf->vector <= T_IRQ_MAX)
{
kprintf("IRQ: %d\n", tf->vector);
//kprintf("IRQ: %d\n", tf->vector);
IRQ_Handler(tf->vector - T_IRQ_BASE);
LAPIC_SendEOI();
@ -149,3 +158,17 @@ trap_entry(TrapFrame *tf)
hlt();
}
void
Debug_Traps(int argc, const char *argv[])
{
int i;
kprintf("Trap Interrupts Trap Interrupts\n");
for (i = 0; i < T_MAX / 2; i++)
{
kprintf("%-4d %-12d %-4d %-12d\n",
i, intStats[i],
T_MAX / 2 + i, intStats[T_MAX / 2 + i]);
}
}

View File

@ -1,11 +1,15 @@
#include <stdint.h>
#include <spinlock.h>
#include "console.h"
#include "x86/vgacons.h"
#include "x86/sercons.h"
#include "x86/debugcons.h"
Spinlock consoleLock;
/*
* Initialize console devices for debugging purposes. At this point interrupts
* and device state is not initialized.
@ -18,6 +22,8 @@ Console_Init()
DebugConsole_Init();
Console_Puts("Castor Operating System\n");
Spinlock_Init(&consoleLock, "Console");
}
/*
@ -66,16 +72,20 @@ Console_Gets(char *str, size_t n)
void
Console_Putc(char ch)
{
Spinlock_Lock(&consoleLock);
VGA_Putc(ch);
Serial_Putc(ch);
DebugConsole_Putc(ch);
Spinlock_Unlock(&consoleLock);
}
void
Console_Puts(const char *str)
{
Spinlock_Lock(&consoleLock);
VGA_Puts(str);
Serial_Puts(str);
DebugConsole_Puts(str);
Spinlock_Unlock(&consoleLock);
}

View File

@ -4,6 +4,7 @@
// Platform Functions
void Debug_Registers(int argc, const char *argv[]);
void Debug_Traps(int argc, const char *argv[]);
void Debug_Reboot(int argc, const char *argv[]);
uintptr_t db_disasm(uintptr_t loc, bool altfmt);

View File

@ -13,7 +13,9 @@ typedef struct Spinlock
char name[SPINLOCK_NAMELEN];
} Spinlock;
void Spinlock_SystemInit();
void Critical_Init();
void Critical_Enter();
void Critical_Exit();
void Spinlock_Init(Spinlock *lock, const char *name);
void Spinlock_Lock(Spinlock *lock);
void Spinlock_Unlock(Spinlock *lock);

View File

@ -172,6 +172,7 @@ Debug_Help(int argc, const char *argv[])
PHELP("dump", "Dump a region of memory");
PHELP("reboot", "Reboot computer");
PHELP("registers", "Show CPU registers");
PHELP("traps", "Trap statistics");
PHELP("help", "Display the list of commands");
}
@ -265,6 +266,8 @@ Debug_Prompt()
Debug_Disasm(argc, (const char **)argv);
} else if (strcmp(argv[0], "reboot") == 0) {
Debug_Reboot(argc, (const char **)argv);
} else if (strcmp(argv[0], "traps") == 0) {
Debug_Traps(argc, (const char **)argv);
} else if (strcmp(argv[0], "echo") == 0) {
Debug_Echo(argc, (const char **)argv);
} else if (strcmp(argv[0], "") != 0) {

View File

@ -11,18 +11,6 @@
#include "../amd64/amd64op.h"
#include "../amd64/atomic.h"
uint32_t lockLevel[MAX_CPUS];
void
Spinlock_SystemInit()
{
int c;
for (c = 0; c < MAX_CPUS; c++)
{
lockLevel[c] = 0;
}
}
void
Spinlock_Init(Spinlock *lock, const char *name)
{
@ -37,8 +25,7 @@ Spinlock_Init(Spinlock *lock, const char *name)
void
Spinlock_Lock(Spinlock *lock)
{
disable_interrupts();
lockLevel[CPU()]++;
Critical_Enter();
while (atomic_swap_uint64(&lock->lock, 1) == 1)
{
@ -55,10 +42,6 @@ Spinlock_Unlock(Spinlock *lock)
atomic_set_uint64(&lock->lock, 0);
lockLevel[CPU()]--;
if (lockLevel[CPU()] == 0)
{
enable_interrupts();
}
Critical_Exit();
}