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

More cleanup and refactoring; SMP initialization fix #325

Merged
merged 4 commits into from
Nov 13, 2023
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
8 changes: 4 additions & 4 deletions arch/x86/pagetables.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ static inline void pgentry_fixup_flags(pgentry_t *entry, unsigned long flags) {
if (unlikely(*entry != entry_new)) {
char flags_str_old[16];
char flags_str_new[16];
printk("WARNING: Already-present PTE protection flags conflicts with our.\n"
" Updating present flags: %s -> %s\n",
dump_pte_flags(flags_str_old, 16, (pte_t) *entry),
dump_pte_flags(flags_str_new, 16, (pte_t) entry_new));
warning("Already-present PTE protection flags conflict with our.\n"
" Updating present flags: %s -> %s",
dump_pte_flags(flags_str_old, 16, (pte_t) *entry),
dump_pte_flags(flags_str_new, 16, (pte_t) entry_new));
*entry = entry_new;
barrier();
flush_tlb();
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/traps.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ void do_exception(cpu_regs_t *regs) {

/* Handle user tasks' exceptions */
if (enter_from_usermode(regs->exc.cs)) {
printk("Task exception: %s\n", panic_str);
warning("Task exception: %s", panic_str);
terminate_user_task();
}

Expand Down
12 changes: 6 additions & 6 deletions common/acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ static int process_madt_entries(void) {

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

percpu_t *percpu = cpu->percpu;
percpu->cpu_id = madt_cpu->apic_proc_id;
Expand Down Expand Up @@ -498,7 +498,7 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) {

cpu_t *cpu =
get_cpu(lapic->ProcessorId) ?: add_cpu(lapic->ProcessorId, false, enabled);
cpu->enabled = enabled;
cpu->flags.enabled = enabled;

percpu_t *percpu = cpu->percpu;
percpu->apic_id = lapic->Id;
Expand Down Expand Up @@ -602,15 +602,15 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) {
cpu_t *cpu =
get_cpu(slapic->ProcessorId) ?: add_cpu(slapic->ProcessorId, false, enabled);

cpu->enabled = enabled;
cpu->flags.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];

if (cpu->enabled) {
if (is_cpu_enabled(cpu)) {
printk("ACPI: [MADT] SAPIC Processor ID: %u, SAPIC ID: %u, SAPIC EID: %u, "
"SAPIC UID: %u, SAPIC UID Str: %c Flags: %08x\n",
cpu->id, slapic->Id, slapic->Eid, slapic->Uid, slapic->UidString[0],
Expand All @@ -627,12 +627,12 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) {
bool enabled = !!(x2lapic->LapicFlags & 0x1);
cpu_t *cpu = get_cpu(x2lapic->Uid) ?: add_cpu(x2lapic->Uid, false, enabled);

cpu->enabled = enabled;
cpu->flags.enabled = enabled;

percpu_t *percpu = cpu->percpu;
percpu->apic_id = x2lapic->LocalApicId;

if (cpu->enabled) {
if (is_cpu_enabled(cpu)) {
printk("ACPI: [MADT] X2APIC Processor ID: %u, APIC ID: %u, Flags: %08x\n",
cpu->id, percpu->apic_id, x2lapic->LapicFlags);
}
Expand Down
6 changes: 3 additions & 3 deletions common/cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ void __text_init cmdline_parse(const char *cmdline) {
strncpy(param->var, optval, param->varlen);
if (strlen(optval) >= param->varlen) {
((char *) param->var)[param->varlen - 1] = '\0';
printk("WARNING: The commandline parameter value for %s does not fit "
"into the preallocated buffer (size %lu >= %u)\n",
param->name, strlen(optval), param->varlen);
warning("The commandline parameter value for %s does not fit "
"into the preallocated buffer (size %lu >= %u)",
param->name, strlen(optval), param->varlen);
}
break;
case ULONG:
Expand Down
6 changes: 3 additions & 3 deletions common/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ static cpu_t bsp = {0};
static void init_cpu(cpu_t *cpu, unsigned int id, bool is_bsp, bool enabled) {
memset(cpu, 0, sizeof(*cpu));
cpu->id = id;
cpu->bsp = is_bsp;
cpu->enabled = enabled;
cpu->flags.bsp = is_bsp;
cpu->flags.enabled = enabled;

init_cpu_runstate(cpu);
if (is_bsp)
Expand Down Expand Up @@ -131,7 +131,7 @@ void wait_for_all_cpus(void) {
cpu_t *cpu;

list_for_each_entry (cpu, &cpus, list) {
if (cpu->bsp)
if (is_cpu_bsp(cpu))
continue;

while (!is_cpu_finished(cpu) || !list_is_empty(&cpu->task_queue))
Expand Down
2 changes: 1 addition & 1 deletion common/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#endif

void reboot(void) {
printk("Rebooting...\n");
warning("Rebooting...");
io_delay();

#ifdef KTF_ACPICA
Expand Down
4 changes: 2 additions & 2 deletions common/pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ static void probe_pci(void) {
const uint32_t vendor_reg = pci_cfg_read(bus, dev, func, PCI_REG_VENDOR);

if (!PCI_DEV_EXISTS(vendor_reg)) {
printk("pci: non-existent host bridge @ 00.0.0\n");
warning("pci: non-existent host bridge @ 00.0.0");
return;
}

class_reg = pci_cfg_read(bus, dev, func, PCI_REG_CLASS);
if (PCI_CLASS(class_reg) != PCI_CLASS_BRIDGE &&
PCI_SUBCLASS(class_reg) != PCI_SUBCLASS_HOST_BRIDGE) {
printk("pci: expected host bridge class code @ 00.0.0\n");
warning("pci: expected host bridge class code @ 00.0.0");
return;
}

Expand Down
2 changes: 2 additions & 0 deletions common/percpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <lib.h>
#include <list.h>
#include <percpu.h>
#include <string.h>

#include <mm/vmm.h>

Expand All @@ -51,6 +52,7 @@ percpu_t *get_percpu_page(unsigned int cpu) {
*/
percpu = get_free_page(GFP_IDENT | GFP_KERNEL | GFP_USER);
BUG_ON(!percpu);
memset(percpu, 0, PAGE_SIZE);
minipli-oss marked this conversation as resolved.
Show resolved Hide resolved

percpu->cpu_id = cpu;

Expand Down
6 changes: 3 additions & 3 deletions common/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ int schedule_task(task_t *task, cpu_t *cpu) {
ASSERT(task);

if (!cpu) {
printk("Unable to schedule task: %s. CPU does not exist.", task->name);
warning("Unable to schedule task: %s. CPU does not exist.", task->name);
return -EEXIST;
}

Expand Down Expand Up @@ -252,7 +252,7 @@ void process_task_repeat(task_t *task) {
void run_tasks(cpu_t *cpu) {
task_t *task, *safe;

if (!cpu->bsp)
if (!is_cpu_bsp(cpu))
wait_cpu_unblocked(cpu);
set_cpu_unfinished(cpu);

Expand All @@ -272,7 +272,7 @@ void run_tasks(cpu_t *cpu) {
}
} while (!list_is_empty(&cpu->task_queue));

if (!cpu->bsp)
if (!is_cpu_bsp(cpu))
set_cpu_blocked(cpu);
set_cpu_finished(cpu);
}
11 changes: 6 additions & 5 deletions common/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ static void __text_init init_vga_console(void) {
void __text_init init_timers(cpu_t *cpu) {
static bool __data_init hpet_initialized = false;

if (cpu->bsp) {
if (is_cpu_bsp(cpu)) {
if (opt_hpet)
hpet_initialized = init_hpet(cpu);

Expand All @@ -178,9 +178,10 @@ void __text_init init_timers(cpu_t *cpu) {
if (opt_apic_timer) {
if (hpet_initialized || opt_pit) /* Needed for APIC timer calibration */
init_apic_timer();
else
printk("CPU%u: Unable to initialize APIC timer - no calibration timers!\n",
cpu->id);
else {
warning("CPU%u: Unable to initialize APIC timer - no calibration timers!",
cpu->id);
}
}
}

Expand Down Expand Up @@ -288,7 +289,7 @@ void __noreturn __text_init kernel_start(uint32_t multiboot_magic, unsigned long

int ret = pfm_initialize();
if (ret != PFM_SUCCESS)
printk("Warning: PFM library initialization failed: %d\n", ret);
warning("PFM library initialization failed: %d", ret);
#endif

/* Jump from .text.init section to .text */
Expand Down
2 changes: 1 addition & 1 deletion common/usermode.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ long syscall_handler(long syscall_nr, long arg1, long arg2, long arg3, long arg4
}

default:
printk("Unknown syscall: %lu\n", syscall_nr);
warning("Unknown syscall: %lu", syscall_nr);
return -1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/hpet.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bool init_hpet(const cpu_t *cpu) {
#endif

if (!hpet) {
printk("HPET not initialized\n");
warning("HPET not initialized");
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions drivers/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void init_keyboard(const cpu_t *cpu) {
/* Controller self test */
outb(KEYBOARD_PORT_CMD, KEYBOARD_CMD_SELF_TEST);
if (inb(KEYBOARD_PORT_DATA) != KEYBOARD_RES_SELF_TEST) {
printk("Self test did not succeed\n");
warning("Self test did not succeed");
return;
}

Expand All @@ -148,7 +148,7 @@ void init_keyboard(const cpu_t *cpu) {

dprintk("Port1 available? %d - port2 available? %d\n", port1, port2);
if (!port1 && !port2) {
printk("No available PS/2 working ports\n");
warning("No available PS/2 working ports");
return;
}

Expand Down
25 changes: 17 additions & 8 deletions include/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,20 @@
#define CPU_UNBLOCKED (1 << 0)
#define CPU_FINISHED (1 << 1)

struct cpu_flags {
uint64_t bsp : 1, enabled : 1, rsvd : 62;
};
typedef struct cpu_flags cpu_flags_t;

struct cpu {
list_head_t list;

unsigned int id;
unsigned int bsp : 1, enabled : 1;

atomic_t run_state;

percpu_t *percpu;

spinlock_t lock;

list_head_t task_queue;
atomic_t run_state;

unsigned int id;
cpu_flags_t flags;
};
typedef struct cpu cpu_t;

Expand All @@ -66,6 +67,14 @@ extern void wait_for_all_cpus(void);

/* Static declarations */

static inline bool is_cpu_bsp(cpu_t *cpu) {
return cpu->flags.bsp;
}

static inline bool is_cpu_enabled(cpu_t *cpu) {
return cpu->flags.enabled;
}

static inline void init_cpu_runstate(cpu_t *cpu) {
atomic_set(&cpu->run_state, 0);
}
Expand Down
6 changes: 3 additions & 3 deletions smp/mptables.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ static void process_mpc_entries(mpc_hdr_t *mpc_ptr) {
cpu = get_cpu(mpc_cpu->lapic_id)
?: add_cpu(mpc_cpu->lapic_id, false, enabled);

cpu->enabled = enabled;
cpu->flags.enabled = enabled;

percpu_t *percpu = cpu->percpu;
percpu->apic_id = mpc_cpu->lapic_id;
Expand Down Expand Up @@ -301,15 +301,15 @@ int init_mptables(void) {
mpc_hdr_t *mpc_ptr;

if (!mpf_ptr) {
printk("No MP Floating Structure Pointer found!\n");
warning("No MP Floating Structure Pointer found!");
return -ENODEV;
}

if (opt_debug)
dump_mpf(mpf_ptr);

if (mpf_ptr->mpc_type > 0 || mpf_ptr->mpc_base == 0x0) {
printk("No MP Configuration Table present!\n");
warning("No MP Configuration Table present!");
return -ENOENT;
}

Expand Down
10 changes: 6 additions & 4 deletions smp/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ void __noreturn ap_startup(void) {
init_traps(cpu);
init_apic(ap_cpuid, apic_get_mode());

ap_callin = true;
smp_wmb();

init_timers(cpu);
sti();

if (opt_fpu)
enable_fpu();

/* Release BSP after full AP initialization */
ap_callin = true;
smp_wmb();

while (true)
run_tasks(cpu);

Expand All @@ -75,7 +76,7 @@ static __text_init void boot_cpu(cpu_t *cpu) {
percpu_t *percpu = cpu->percpu;
apic_icr_t icr;

if (cpu->bsp)
if (is_cpu_bsp(cpu))
return;

ap_new_sp = get_free_pages_top(PAGE_ORDER_2M, GFP_KERNEL_MAP);
Expand Down Expand Up @@ -103,6 +104,7 @@ static __text_init void boot_cpu(cpu_t *cpu) {

apic_wait_ready();

/* Wait for AP initialization */
while (!ap_callin)
cpu_relax();

Expand Down
2 changes: 1 addition & 1 deletion tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static get_next_test_result_t get_next_test(test_fn **out_test_fn, char **out_na
if (*out_name) {
*out_test_fn = symbol_address(*out_name);
if (!*out_test_fn) {
printk("Symbol for test %s not found\n", *out_name);
warning("Symbol for test %s not found", *out_name);
return TESTS_ERROR;
}
return TESTS_FOUND;
Expand Down
Loading