Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smp: add an abstraction handling detected CPUs' metadata #266

Merged
merged 2 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions arch/x86/apic.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
#include <apic.h>
#include <console.h>
#include <cpu.h>
#include <drivers/pit.h>
#include <ktf.h>
#include <lib.h>
Expand Down Expand Up @@ -107,10 +108,10 @@ void apic_icr_write(const apic_icr_t *icr) {

apic_mode_t apic_get_mode(void) { return apic_mode; }

void init_apic(unsigned int cpu, apic_mode_t mode) {
percpu_t *percpu = get_percpu_page(cpu);
void init_apic(unsigned int cpu_id, apic_mode_t mode) {
apic_base_t apic_base;
apic_spiv_t spiv;
cpu_t *cpu;

BUG_ON(mode < APIC_MODE_DISABLED);

Expand All @@ -128,8 +129,8 @@ void init_apic(unsigned int cpu, apic_mode_t mode) {
BUG();
}

printk("CPU%u: Initializing APIC mode: %s -> %s\n", cpu, apic_mode_names[apic_mode],
apic_mode_names[mode]);
printk("CPU%u: Initializing APIC mode: %s -> %s\n", cpu_id,
apic_mode_names[apic_mode], apic_mode_names[mode]);

/* Disable APIC */
apic_base.en = apic_base.extd = 0;
Expand All @@ -146,8 +147,11 @@ void init_apic(unsigned int cpu, apic_mode_t mode) {
}

apic_mode = mode;
percpu->apic_base = apic_base;
PERCPU_SET(bsp, apic_base.bsp);

cpu = !!apic_base.bsp ? get_bsp_cpu() : get_cpu(cpu_id);
BUG_ON(!cpu || cpu->id != cpu_id);

cpu->percpu->apic_base = apic_base;

/* XAPIC requires MMIO accesses, thus the APIC_BASE page needs to be mapped.
* X2APIC uses MSRs for accesses, so no mapping needed.
Expand Down
6 changes: 3 additions & 3 deletions arch/x86/traps.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ static void init_gdt(percpu_t *percpu) {
init_tss(percpu);
}

void init_traps(unsigned int cpu) {
percpu_t *percpu = get_percpu_page(cpu);
void init_traps(const cpu_t *cpu) {
percpu_t *percpu = cpu->percpu;

BUG_ON(!percpu);

Expand Down Expand Up @@ -175,7 +175,7 @@ void init_traps(unsigned int cpu) {

init_gdt(percpu);

wrmsr(MSR_TSC_AUX, cpu);
wrmsr(MSR_TSC_AUX, cpu->id);
}

static void dump_general_regs(const struct cpu_regs *regs) {
Expand Down
83 changes: 37 additions & 46 deletions common/acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <acpi_ktf.h>
#include <cpu.h>
#include <ioapic.h>
#include <percpu.h>
#include <setup.h>
#include <string.h>

Expand All @@ -46,10 +46,6 @@ static const char *madt_int_trigger_names[] = {
[ACPI_MADT_INT_TRIGGER_LT] = "Level",
};

static unsigned nr_cpus;

unsigned acpi_get_nr_cpus(void) { return nr_cpus; }

#ifndef KTF_ACPICA
#include <errno.h>
#include <page.h>
Expand Down Expand Up @@ -232,7 +228,7 @@ static int process_fadt(void) {
return 0;
}

static int process_madt_entries(unsigned bsp_cpu_id) {
static int process_madt_entries(void) {
acpi_madt_t *madt = (acpi_madt_t *) acpi_find_table(MADT_SIGNATURE);
acpi_madt_entry_t *entry;
bus_t *isa_bus = NULL;
Expand All @@ -257,23 +253,23 @@ static int process_madt_entries(unsigned bsp_cpu_id) {
switch (entry->type) {
case ACPI_MADT_TYPE_LAPIC: {
acpi_madt_processor_t *madt_cpu = (acpi_madt_processor_t *) entry->data;
percpu_t *percpu;
bool enabled;

/* Some systems report all CPUs, marked as disabled */
enabled = !!(madt_cpu->flags & 0x1);
if (!enabled)
break;

percpu = get_percpu_page(madt_cpu->apic_proc_id);
cpu_t *cpu = get_cpu(madt_cpu->apic_proc_id)
?: add_cpu(madt_cpu->apic_proc_id, false, enabled);
cpu->enabled = enabled;

percpu_t *percpu = cpu->percpu;
percpu->cpu_id = madt_cpu->apic_proc_id;
percpu->apic_id = madt_cpu->apic_id;
percpu->bsp = !!(madt_cpu->apic_proc_id == bsp_cpu_id);
percpu->enabled = enabled;

nr_cpus++;
printk("ACPI: [MADT] APIC Processor ID: %u, APIC ID: %u, Flags: %08x\n",
madt_cpu->apic_proc_id, madt_cpu->apic_id, madt_cpu->flags);
cpu->id, percpu->apic_id, madt_cpu->flags);
break;
}
case ACPI_MADT_TYPE_IOAPIC: {
Expand Down Expand Up @@ -383,7 +379,7 @@ acpi_table_t *acpi_find_table(uint32_t signature) {
return NULL;
}

int init_acpi(unsigned bsp_cpu_id) {
int init_acpi(void) {
unsigned acpi_nr_tables;
rsdt_t *rsdt = NULL;
xsdt_t *xsdt = NULL;
Expand Down Expand Up @@ -437,7 +433,7 @@ int init_acpi(unsigned bsp_cpu_id) {
if (rc < 0)
return rc;

return process_madt_entries(bsp_cpu_id);
return process_madt_entries();
}
#else /* KTF_ACPICA */
#include "acpi.h"
Expand Down Expand Up @@ -486,24 +482,22 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) {
switch (entry->Type) {
case ACPI_MADT_TYPE_LOCAL_APIC: {
ACPI_MADT_LOCAL_APIC *lapic = (ACPI_MADT_LOCAL_APIC *) entry;
uint32_t bsp_cpu_id = (uint32_t) _ul(arg);
percpu_t *percpu;
bool enabled;

/* Some systems report all CPUs, marked as disabled */
enabled = !!(lapic->LapicFlags & 0x1);
if (!enabled)
break;

percpu = get_percpu_page(lapic->ProcessorId);
percpu->cpu_id = lapic->ProcessorId;
cpu_t *cpu =
get_cpu(lapic->ProcessorId) ?: add_cpu(lapic->ProcessorId, false, enabled);
cpu->enabled = enabled;

percpu_t *percpu = cpu->percpu;
percpu->apic_id = lapic->Id;
percpu->bsp = !!(lapic->ProcessorId == bsp_cpu_id);
percpu->enabled = enabled;

nr_cpus++;
printk("ACPI: [MADT] APIC Processor ID: %u, APIC ID: %u, Flags: %08x\n",
percpu->cpu_id, percpu->apic_id, lapic->LapicFlags);
printk("ACPI: [MADT] APIC Processor ID: %u, APIC ID: %u, Flags: %08x\n", cpu->id,
percpu->apic_id, lapic->LapicFlags);
break;
}
case ACPI_MADT_TYPE_IO_APIC: {
Expand Down Expand Up @@ -597,25 +591,23 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) {
}
case ACPI_MADT_TYPE_LOCAL_SAPIC: {
ACPI_MADT_LOCAL_SAPIC *slapic = (ACPI_MADT_LOCAL_SAPIC *) entry;
percpu_t *percpu = get_percpu_page(slapic->ProcessorId);
uint32_t bsp_cpu_id = (uint32_t) _ul(arg);
bool enabled = !!(slapic->LapicFlags & 0x1);
cpu_t *cpu =
get_cpu(slapic->ProcessorId) ?: add_cpu(slapic->ProcessorId, false, enabled);

percpu->cpu_id = slapic->ProcessorId;
cpu->enabled = enabled;

percpu_t *percpu = cpu->percpu;
percpu->sapic_id = slapic->Id;
percpu->sapic_eid = slapic->Eid;

percpu->sapic_uid = slapic->Uid;
percpu->sapic_uid_str[0] = slapic->UidString[0];

percpu->bsp = !!(slapic->ProcessorId == bsp_cpu_id);
percpu->enabled = !!(slapic->LapicFlags & 0x1);

if (percpu->enabled) {
nr_cpus++;
if (cpu->enabled) {
printk("ACPI: [MADT] SAPIC Processor ID: %u, SAPIC ID: %u, SAPIC EID: %u, "
"SAPIC UID: %u, SAPIC UID Str: %c Flags: %08x\n",
percpu->cpu_id, slapic->Id, slapic->Eid, slapic->Uid,
slapic->UidString[0], slapic->LapicFlags);
cpu->id, slapic->Id, slapic->Eid, slapic->Uid, slapic->UidString[0],
slapic->LapicFlags);
}
break;
}
Expand All @@ -625,18 +617,17 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) {
}
case ACPI_MADT_TYPE_LOCAL_X2APIC: {
ACPI_MADT_LOCAL_X2APIC *x2lapic = (ACPI_MADT_LOCAL_X2APIC *) entry;
percpu_t *percpu = get_percpu_page(x2lapic->Uid);
uint32_t bsp_cpu_id = (uint32_t) _ul(arg);
bool enabled = !!(x2lapic->LapicFlags & 0x1);
cpu_t *cpu = get_cpu(x2lapic->Uid) ?: add_cpu(x2lapic->Uid, false, enabled);

cpu->enabled = enabled;

percpu->cpu_id = x2lapic->Uid;
percpu_t *percpu = cpu->percpu;
percpu->apic_id = x2lapic->LocalApicId;
percpu->bsp = !!(x2lapic->Uid == bsp_cpu_id);
percpu->enabled = !!(x2lapic->LapicFlags & 0x1);

if (percpu->enabled) {
nr_cpus++;
if (cpu->enabled) {
printk("ACPI: [MADT] X2APIC Processor ID: %u, APIC ID: %u, Flags: %08x\n",
percpu->cpu_id, percpu->apic_id, x2lapic->LapicFlags);
cpu->id, percpu->apic_id, x2lapic->LapicFlags);
}
break;
}
Expand Down Expand Up @@ -703,7 +694,7 @@ static ACPI_STATUS init_fadt(void) {
return AE_OK;
}

static ACPI_STATUS init_madt(unsigned bsp_cpu_id) {
static ACPI_STATUS init_madt(void) {
ACPI_TABLE_MADT *madt = acpi_find_table(ACPI_SIG_MADT);
ACPI_SUBTABLE_HEADER *subtbl = (void *) madt + sizeof(*madt);

Expand All @@ -712,7 +703,7 @@ static ACPI_STATUS init_madt(unsigned bsp_cpu_id) {

uint32_t length = madt->Header.Length - sizeof(*madt);

acpi_walk_subtables(subtbl, length, madt_parser, (void *) _ul(bsp_cpu_id));
acpi_walk_subtables(subtbl, length, madt_parser, NULL);
return AE_OK;
}

Expand All @@ -733,7 +724,7 @@ void acpi_walk_subtables(ACPI_SUBTABLE_HEADER *entry, uint32_t length,
}
}

ACPI_STATUS init_acpi(unsigned bsp_cpu_id) {
ACPI_STATUS init_acpi(void) {
ACPI_STATUS status;

printk("Initializing ACPI support\n");
Expand All @@ -746,7 +737,7 @@ ACPI_STATUS init_acpi(unsigned bsp_cpu_id) {
if (status != AE_OK)
return status;

status = init_madt(bsp_cpu_id);
status = init_madt();
return status;
}

Expand Down
112 changes: 112 additions & 0 deletions common/cpu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright © 2022 Open Source Security, Inc.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <console.h>
#include <cpu.h>
#include <ktf.h>
#include <lib.h>
#include <list.h>
#include <spinlock.h>
#include <string.h>

#include <mm/slab.h>

static list_head_t cpus;
static unsigned int nr_cpus = 0;

static cpu_t bsp = {0};

static void init_cpu(cpu_t *cpu, unsigned int id, bool bsp, bool enabled) {
memset(cpu, 0, sizeof(*cpu));
cpu->id = id;
cpu->bsp = bsp;
cpu->enabled = enabled;
cpu->done = false;

cpu->percpu = get_percpu_page(id);
BUG_ON(!cpu->percpu);

cpu->lock = SPINLOCK_INIT;
}

cpu_t *init_cpus(void) {
printk("Initialize CPU structures\n");

list_init(&cpus);

init_cpu(&bsp, 0, true, true);
list_add(&bsp.list, &cpus);
nr_cpus = 1;

return &bsp;
}

cpu_t *add_cpu(unsigned int id, bool bsp, bool enabled) {
cpu_t *cpu = kzalloc(sizeof(*cpu));

if (!cpu)
return NULL;

init_cpu(cpu, id, bsp, enabled);

list_add(&cpu->list, &cpus);
nr_cpus++;

return cpu;
}

cpu_t *get_cpu(unsigned int id) {
cpu_t *cpu;

list_for_each_entry (cpu, &cpus, list) {
if (cpu->id == id)
return cpu;
}

return NULL;
}

unsigned int get_nr_cpus(void) { return nr_cpus; }

cpu_t *get_bsp_cpu(void) { return &bsp; }

void for_each_cpu(void (*func)(cpu_t *cpu)) {
cpu_t *cpu;

list_for_each_entry (cpu, &cpus, list)
func(cpu);
}

void wait_for_all_cpus(void) {
cpu_t *cpu, *safe;

do {
list_for_each_entry_safe (cpu, safe, &cpus, list) {
spin_lock(&cpu->lock);
if (cpu->done)
list_unlink(&cpu->list);
spin_unlock(&cpu->lock);
}
} while (!list_is_empty(&cpus));
}
Loading