diff --git a/common/acpi.c b/common/acpi.c index dea0b689..149cb124 100644 --- a/common/acpi.c +++ b/common/acpi.c @@ -221,7 +221,7 @@ static const char *madt_int_trigger_names[] = { [ACPI_MADT_INT_TRIGGER_LT] = "Level", }; -static int process_madt_entries(void) { +static int process_madt_entries(unsigned bsp_cpu_id) { acpi_madt_t *madt = (acpi_madt_t *) acpi_find_table(MADT_SIGNATURE); acpi_madt_entry_t *entry; bus_t *isa_bus; @@ -246,11 +246,10 @@ static int process_madt_entries(void) { acpi_madt_processor_t *madt_cpu = (acpi_madt_processor_t *) entry->data; percpu_t *percpu = get_percpu_page(madt_cpu->apic_proc_id); - percpu->id = madt_cpu->apic_proc_id; + percpu->cpu_id = madt_cpu->apic_proc_id; percpu->apic_id = madt_cpu->apic_id; - percpu->enabled = madt_cpu->flags & 0x1; - if (madt_cpu->apic_proc_id == 0) - percpu->bsp = true; + percpu->bsp = !!(madt_cpu->apic_proc_id == bsp_cpu_id); + percpu->enabled = !!(madt_cpu->flags & 0x1); if (!percpu->enabled) continue; @@ -367,7 +366,7 @@ acpi_table_t *acpi_find_table(uint32_t signature) { return NULL; } -int init_acpi(void) { +int init_acpi(unsigned bsp_cpu_id) { unsigned acpi_nr_tables; rsdt_t *rsdt = NULL; xsdt_t *xsdt = NULL; @@ -415,5 +414,5 @@ int init_acpi(void) { } acpi_dump_tables(); - return process_madt_entries(); + return process_madt_entries(bsp_cpu_id); } diff --git a/common/percpu.c b/common/percpu.c index e7f1944b..e732d68d 100644 --- a/common/percpu.c +++ b/common/percpu.c @@ -42,7 +42,7 @@ percpu_t *get_percpu_page(unsigned int cpu) { percpu_t *percpu; list_for_each_entry (percpu, &percpu_frames, list) { - if (percpu->id == cpu) + if (percpu->cpu_id == cpu) return percpu; } @@ -52,7 +52,7 @@ percpu_t *get_percpu_page(unsigned int cpu) { percpu = get_free_page(GFP_IDENT | GFP_KERNEL); BUG_ON(!percpu); - percpu->id = cpu; + percpu->cpu_id = cpu; percpu->user_stack = get_free_page_top(GFP_USER); list_add(&percpu->list, &percpu_frames); diff --git a/common/setup.c b/common/setup.c index 23bddf32..324b2efe 100644 --- a/common/setup.c +++ b/common/setup.c @@ -55,9 +55,14 @@ bool_cmd("debug", opt_debug); io_port_t com_ports[2] = {COM1_PORT, COM2_PORT}; +static unsigned bsp_cpu_id = 0; + const char *kernel_cmdline; char cpu_identifier[49]; +unsigned get_bsp_cpu_id(void) { return bsp_cpu_id; } +void set_bsp_cpu_id(unsigned cpu_id) { bsp_cpu_id = cpu_id; } + static __text_init int parse_bool(const char *s) { if (!strcmp("no", s) || !strcmp("off", s) || !strcmp("false", s) || !strcmp("disable", s) || !strcmp("0", s)) @@ -208,11 +213,11 @@ void __noreturn __text_init kernel_start(uint32_t multiboot_magic, init_percpu(); - init_traps(0); + init_traps(get_bsp_cpu_id()); init_slab(); - if (init_acpi() < 0 && init_mptables() < 0) { + if (init_acpi(get_bsp_cpu_id()) < 0 && init_mptables() < 0) { BUG(); } @@ -225,10 +230,10 @@ void __noreturn __text_init kernel_start(uint32_t multiboot_magic, init_ioapic(); /* Initialize console input */ - uart_input_init(0); + uart_input_init(get_bsp_cpu_id()); /* Initialize Programmable Interrupt Timer */ - init_pit(0); + init_pit(get_bsp_cpu_id()); /* Jump from .text.init section to .text */ asm volatile("push %0; ret" ::"r"(&kernel_main)); diff --git a/include/acpi.h b/include/acpi.h index 91418539..911e7042 100644 --- a/include/acpi.h +++ b/include/acpi.h @@ -249,6 +249,6 @@ typedef struct acpi_madt acpi_madt_t; extern acpi_table_t *acpi_find_table(uint32_t signature); extern unsigned acpi_get_nr_cpus(void); -extern int init_acpi(void); +extern int init_acpi(unsigned bsp_cpu_id); #endif /* KTF_ACPI_H */ diff --git a/include/percpu.h b/include/percpu.h index 8a1883f8..d4ed8a98 100644 --- a/include/percpu.h +++ b/include/percpu.h @@ -32,8 +32,14 @@ struct percpu { list_head_t list; - unsigned int id : 8, apic_id : 8, enabled : 1, bsp : 1, family : 4, model : 4, - stepping : 4; + + unsigned int cpu_id; + uint32_t apic_id; + bool enabled; + bool bsp; + uint8_t family; + uint8_t model; + uint8_t stepping; idt_entry_t *idt __aligned(16); idt_ptr_t idt_ptr; diff --git a/include/setup.h b/include/setup.h index f5a01215..c8dfd68c 100644 --- a/include/setup.h +++ b/include/setup.h @@ -40,6 +40,8 @@ extern io_port_t com_ports[2]; extern const char *kernel_cmdline; extern char cpu_identifier[49]; +/* Static declarations */ + static inline void get_com_ports(void) { memcpy((void *) com_ports, (void *) (BDA_COM_PORTS_ENTRY), sizeof(com_ports)); @@ -50,6 +52,11 @@ static inline void get_com_ports(void) { com_ports[1] = COM2_PORT; } +/* External declarations */ + +extern unsigned get_bsp_cpu_id(void); +extern void set_bsp_cpu_id(unsigned cpu_id); + extern void zap_boot_mappings(void); #endif /* __ASSEMBLY__ */ diff --git a/smp/mptables.c b/smp/mptables.c index a63c24dd..b1485d89 100644 --- a/smp/mptables.c +++ b/smp/mptables.c @@ -210,12 +210,15 @@ static void process_mpc_entries(mpc_hdr_t *mpc_ptr) { percpu_t *percpu = get_percpu_page(nr_cpus++); percpu->apic_id = mpc_cpu->lapic_id; - percpu->enabled = mpc_cpu->en; - percpu->bsp = mpc_cpu->bsp; + percpu->enabled = !!mpc_cpu->en; + percpu->bsp = !!mpc_cpu->bsp; percpu->family = mpc_cpu->family; percpu->model = mpc_cpu->model; percpu->stepping = mpc_cpu->stepping; + if (percpu->bsp) + set_bsp_cpu_id(percpu->cpu_id); + dump_mpc_processor_entry(mpc_cpu); entry_ptr += sizeof(*mpc_cpu); break; diff --git a/smp/smp.c b/smp/smp.c index 9a6edbba..6e872fce 100644 --- a/smp/smp.c +++ b/smp/smp.c @@ -94,13 +94,13 @@ static __text_init void boot_cpu(unsigned int cpu) { } void __text_init init_smp(void) { - unsigned mp_nr_cpus = mptables_get_nr_cpus(); - unsigned acpi_nr_cpus = acpi_get_nr_cpus(); - - nr_cpus = acpi_nr_cpus ?: mp_nr_cpus; + nr_cpus = acpi_get_nr_cpus(); if (nr_cpus == 0) { - nr_cpus++; - return; + nr_cpus = mptables_get_nr_cpus(); + if (nr_cpus == 0) { + nr_cpus++; + return; + } } printk("Initializing SMP support (CPUs: %u)\n", nr_cpus);