From ef4943053475cd8bf341c42dd0b538bc630b92a3 Mon Sep 17 00:00:00 2001 From: Miguel Date: Thu, 6 Sep 2018 01:58:10 +0200 Subject: working on smp --- Makefile | 1 + README.md | 2 + asm/asm_int.s | 49 ++++++++++--------- asm/asm_mp.asm | 6 ++- asm/asm_x86.s | 6 +-- driver/serial.c | 5 +- kernel/acpi.c | 20 +++++--- kernel/interrupts.c | 130 +++++++++++++++++++++++++++++++------------------- kernel/kernel.c | 1 - kernel/kernel.h | 1 + kernel/log.c | 6 +++ kernel/smp.c | 28 +++++++---- kernel/spinlock.c | 13 +---- kernel/spinlock.h | 12 ++++- userspace/exception.c | 4 ++ 15 files changed, 179 insertions(+), 105 deletions(-) create mode 100644 userspace/exception.c diff --git a/Makefile b/Makefile index 42723ae..6aba498 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,7 @@ CFLAGS+=-O0 CFLAGS+=-I. CFLAGS+=-I/home/miguel/temp/foolos/usr/i686-foolos/include/ CFLAGS+=-I./asm +CFLAGS+=-I./kernel CFLAGS+=-gstabs #CFLAGS+=-fstack-protector-all diff --git a/README.md b/README.md index fdd4079..565a742 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,8 @@ Discontinued Features Todos ----- +* cpuid +* lapic / spurious . ioapic * newlib reentrant struct!! * Interrupts between BSP and APS ? * Do not forget to flush TLB diff --git a/asm/asm_int.s b/asm/asm_int.s index 566b646..94785bc 100644 --- a/asm/asm_int.s +++ b/asm/asm_int.s @@ -103,7 +103,7 @@ push \num // pass in this interrupt number push %eax // pass in original %esp (saved just few lines before) - call \func + call \func // call aligned mov %eax,%esp // use the %esp we got from c function pop %gs // pop everything back... @@ -125,8 +125,13 @@ .endm -.macro excx func - call \func +.macro excx num func + mov %esp,%eax // remember THIS stack position + and $-16,%esp // padding to align stack on 16byte boundary before CALL + sub $8,%esp // ... + push \num // pass in this interrupt number + push %eax // pass in original %esp (saved just few lines before) + call \func // call aligned jmp . .endm @@ -153,22 +158,22 @@ int129: intx ack0 $129 interrupt_handler int255: intx ack0 $255 interrupt_handler -exc0: excx exception_handle_0 -exc1: excx exception_handle_1 -exc2: excx exception_handle_2 -exc3: excx exception_handle_3 -exc4: excx exception_handle_4 -exc5: excx exception_handle_5 -exc6: excx exception_handle_6 -exc7: excx exception_handle_7 -exc8: excx exception_handle_8 -exc9: excx exception_handle_9 -exc10: excx exception_handle_10 -exc11: excx exception_handle_11 -exc12: excx exception_handle_12 -exc13: excx exception_handle_13 -exc14: excx exception_handle_14 -exc15: excx exception_handle_15 -exc16: excx exception_handle_16 -exc17: excx exception_handle_17 -exc18: excx exception_handle_18 +exc0: excx $0 exception_handle +exc1: excx $1 exception_handle +exc2: excx $2 exception_handle +exc3: excx $3 exception_handle +exc4: excx $4 exception_handle +exc5: excx $5 exception_handle +exc6: excx $6 exception_handle +exc7: excx $7 exception_handle +exc8: excx $8 exception_handle +exc9: excx $9 exception_handle +exc10: excx $10 exception_handle +exc11: excx $11 exception_handle +exc12: excx $12 exception_handle +exc13: excx $13 exception_handle +exc14: excx $14 exception_handle +exc15: excx $15 exception_handle +exc16: excx $16 exception_handle +exc17: excx $17 exception_handle +exc18: excx $18 exception_handle diff --git a/asm/asm_mp.asm b/asm/asm_mp.asm index c4eb4a9..a2dcee0 100644 --- a/asm/asm_mp.asm +++ b/asm/asm_mp.asm @@ -1,7 +1,8 @@ global smp_start +global LLOCK extern smp_main + ; master boot record for application processors -;[org 0x7000] smp_start: [bits 16] @@ -41,6 +42,9 @@ boot_32_pm: cmp eax,1 je boot_32_pm + mov ebp, 0x7000 + mov esp, ebp + call smp_main jmp $ ; should never be reached diff --git a/asm/asm_x86.s b/asm/asm_x86.s index 35052b1..3481ec2 100644 --- a/asm/asm_x86.s +++ b/asm/asm_x86.s @@ -123,9 +123,9 @@ set_cr4: ret x86_xchg: - mov 8(%esp), %eax // addr - mov 4(%esp), %edx // value - xchg %edx, (%eax) //LOCK protocol impemented anyway + mov 8(%esp), %eax // value + mov 4(%esp), %edx // addr + xchg %eax, (%edx) //LOCK protocol impemented anyway ret x86_invlpg: diff --git a/driver/serial.c b/driver/serial.c index 8a5dd2e..4e43251 100644 --- a/driver/serial.c +++ b/driver/serial.c @@ -1,6 +1,5 @@ -#include "driver/serial.h" - -#include "asm_x86.h" // provides x86_inb() and x86_outb() +#include "serial.h" +#include "asm_x86.h" // provides x86_inb() and x86_outb() /** COM1 Port */ static const PORT=0x3f8; diff --git a/kernel/acpi.c b/kernel/acpi.c index 084c193..f5c18ca 100644 --- a/kernel/acpi.c +++ b/kernel/acpi.c @@ -62,7 +62,7 @@ uint8_t *apci_get_next_entry(uint8_t *addr,smp_processors *procdata) if(*addr==0) { - klog("MADT Entry: LocalAPIC ()"); + klog("Type 0: LocalAPIC (enabled=%d)",addr[4]&1); // usable if(addr[4]&1) { @@ -78,10 +78,18 @@ uint8_t *apci_get_next_entry(uint8_t *addr,smp_processors *procdata) } else if(*addr==1) { - klog("MADT Entry: IO APIC 0x%08X",addr[4]); + klog("Type 1: IO APIC (id=%d) (addr=0x%08X) (base=%d)",addr[2], *((uint32_t*)&addr[4]),*((uint32_t*)&addr[8])); } - else if(*addr==2)klog("MADT Entry: Interrupt Source Override"); - else klog("MADT Entry: type:0x%X",*addr); + else if(*addr==2){ + klog("Type 2: Interrupt Source Override (bus src=%d) (irq src=%d) (global=%d) (flags=%d)",addr[2],addr[3],*((uint32_t*)&addr[4]),*((uint16_t*)&addr[8])); + + } + else if(*addr==4){ + klog("Type 4: Non-maskable interrupts (proc id=%d) (flags=%d) (LINT#=%d)",addr[2],*((uint16_t*)&addr[3]),addr[5]); + + } + else if(*addr==5){kpanic("Entry Type 5 : Local APIC Address Override. 64-bit address??");} + else kpanic("MADT Entry: type:0x%X",*addr); return addr+addr[1]; } @@ -102,14 +110,14 @@ void acpi_check_madt(uint32_t *madt,smp_processors *procdata) entry+=sizeof(acpi_madt); procdata->local_apic_address=table->apic_local; + klog("Local Apic Address: 0x%08X",table->apic_local); procdata->flags=table->flags; + klog("Flags (Dual 8259): %d",table->flags); while(entry - #include "kernel/kernel.h" #include "kernel/mem.h" diff --git a/kernel/kernel.h b/kernel/kernel.h index f73c116..4c480e5 100644 --- a/kernel/kernel.h +++ b/kernel/kernel.h @@ -18,6 +18,7 @@ #define KMALLOC_MEM_SIZE 1024*1024*8 // 8MB for in kernel-memory #define NUMBER_SPINLOCKS 16 +#define SPINLOCK_LOG 0 #define S1(x) #x #define S2(x) S1(x) diff --git a/kernel/log.c b/kernel/log.c index c7c1bb7..b0eeab5 100644 --- a/kernel/log.c +++ b/kernel/log.c @@ -1,8 +1,12 @@ #include "log.h" +#include "kernel.h" + #include #include +#include "spinlock.h" + #include "kernel/kernel.h" #include "kernel/fifo.h" #include "driver/serial.h" @@ -43,7 +47,9 @@ void log(char *module_name, int prio, char *format_string, ...) tfp_sprintf(buf_log,"\033[36;40m%s\033[31;40m %s:\033[37;40m %s\n",buf_time,module_name,buf_info); + spinlock_spin(SPINLOCK_LOG); log_string(buf_log); + spinlock_release(SPINLOCK_LOG); } /* diff --git a/kernel/smp.c b/kernel/smp.c index 4aff013..ea926ca 100644 --- a/kernel/smp.c +++ b/kernel/smp.c @@ -20,29 +20,40 @@ volatile uint8_t proc; uint32_t cpu_counter[SMP_MAX_PROC]; uint32_t local_apic_addr; +extern uint32_t LLOCK; void smp_main() { + uint32_t ebp=kballoc(1); + asm volatile("mov %0, %%ebp"::"r"(ebp)); + asm volatile("mov %ebp, %esp"); + asm volatile("jmp kernel_ap"); +} -// klog("local apic_addr:0x%08X",local_apic_addr); +void kernel_ap() +{ + LLOCK=0; - // // uint32_t *reg=local_apic_addr+FOOLOS_APIC_ID; - //klog("local apic id: 0x%08X",(*reg)); + uint32_t *reg; + reg=local_apic_addr+FOOLOS_APIC_ID; + klog("smp local apic id: %d",(*reg)); // // // *reg=local_apic_addr+FOOLOS_APIC_SPUR_INT; // // // *reg|=0x100;//0xffffffff; // all bits 1 and interrupt 255 // *reg=0;//xffffffff; // all bits 1 and interrupt 255 int_install(); - + x86_sti(); - - - while(1)__asm__("hlt"); + while(1){ + klog("%d",*reg); + for(int i=0;i<1000000000;i++); + } // switch_to_user_mode(); // int x=1/0; + /* while(1); @@ -54,10 +65,11 @@ void smp_main() proc=c1=c2=c3=0; for(int i=0;i -void lock_spin(spinlock); -void lock_release(spinlock); +void spinlock_spin(uint32_t num); +void spinlock_release(uint32_t num); #endif diff --git a/userspace/exception.c b/userspace/exception.c new file mode 100644 index 0000000..4f72c86 --- /dev/null +++ b/userspace/exception.c @@ -0,0 +1,4 @@ +int main() +{ + return 10/0; +} -- cgit v1.2.3