Skip to content

Commit

Permalink
setup: track BSP CPU ID via common interface
Browse files Browse the repository at this point in the history
Add get_bsp_cpu_id() and set_bsp_cpu_id() helper functions.
Update bsp variable on BSP's percpu page upon both ACPI and MP tables
enumeration.

Do not use bitfield for storing percpu variables. And also rename id
to cpu_id.

Signed-off-by: Pawel Wieczorkiewicz <[email protected]>
  • Loading branch information
wipawel committed Oct 28, 2020
1 parent f379bfa commit 1d4e794
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 24 deletions.
13 changes: 6 additions & 7 deletions common/acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -415,5 +414,5 @@ int init_acpi(void) {
}

acpi_dump_tables();
return process_madt_entries();
return process_madt_entries(bsp_cpu_id);
}
4 changes: 2 additions & 2 deletions common/percpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down
13 changes: 9 additions & 4 deletions common/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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();
}

Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion include/acpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
10 changes: 8 additions & 2 deletions include/percpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions include/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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__ */
Expand Down
7 changes: 5 additions & 2 deletions smp/mptables.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions smp/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1d4e794

Please sign in to comment.