Skip to content

Commit a462e2f

Browse files
committed
percpu,drivers: Add cpu self reference to percpu
Adding a self reference to the percpu allows checking what CPU some code is currently running on. We use this in pb_set_handler to ensure it runs on the bsp.
1 parent 9751262 commit a462e2f

File tree

4 files changed

+10
-0
lines changed

4 files changed

+10
-0
lines changed

common/cpu.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static void init_cpu(cpu_t *cpu, unsigned int id, bool is_bsp, bool enabled) {
4949

5050
cpu->percpu = get_percpu_page(id);
5151
BUG_ON(!cpu->percpu);
52+
cpu->percpu->cpu = cpu;
5253

5354
cpu->lock = SPINLOCK_INIT;
5455
list_init(&cpu->task_queue);

drivers/power_button.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ static void default_handler(void *notused) {
2424
}
2525

2626
void pb_set_handler(pb_handler_t handler, void *context) {
27+
// check that we are on the bsp. Assumes that there is no task migration
28+
ASSERT(is_cpu_bsp(get_this_cpu()));
29+
2730
unsigned long flags = interrupts_disable_save();
2831
pb_handler = handler;
2932
pb_context = context;

include/cpu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ extern void wait_for_all_cpus(void);
6767

6868
/* Static declarations */
6969

70+
static inline cpu_t * get_this_cpu() {
71+
return PERCPU_GET(cpu);
72+
}
73+
7074
static inline bool is_cpu_bsp(cpu_t *cpu) {
7175
return cpu->flags.bsp;
7276
}

include/percpu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <list.h>
3232
#include <page.h>
3333

34+
struct cpu;
3435
struct percpu {
3536
list_head_t list;
3637

@@ -60,6 +61,7 @@ struct percpu {
6061
unsigned long usermode_private;
6162
volatile unsigned long apic_ticks;
6263
bool apic_timer_enabled;
64+
struct cpu *cpu;
6365
} __aligned(PAGE_SIZE);
6466
typedef struct percpu percpu_t;
6567

0 commit comments

Comments
 (0)