2 * Copyright (c) 2009 The Regents of the University of California
3 * Barret Rhoden <brho@cs.berkeley.edu>
4 * See LICENSE for details.
10 /* SMP related functions */
13 #include <ros/common.h>
14 #include <sys/queue.h>
22 #define CPU_STATE_IRQ 0
23 #define CPU_STATE_KERNEL 1
24 #define CPU_STATE_USER 2
25 #define CPU_STATE_IDLE 3
26 #define NR_CPU_STATES 4
28 static char *cpu_state_names[NR_CPU_STATES] =
38 uintptr_t stacktop; /* must be first */
39 int coreid; /* must be second */
41 pseudodesc_t host_gdt;
46 /* Process management */
47 // cur_proc should be valid on all cores that are not management cores.
48 struct proc *cur_proc; /* which process context is loaded */
49 struct proc *owning_proc; /* proc owning the core / cur_ctx */
50 uint32_t owning_vcoreid; /* vcoreid of owning proc (if applicable */
51 struct user_context *cur_ctx; /* user ctx we came in on (can be 0) */
52 struct user_context actual_ctx; /* storage for cur_ctx */
53 uint32_t __ctx_depth; /* don't access directly. see trap.h. */
54 int __lock_checking_enabled;/* == 1, enables spinlock depth checking */
55 struct kthread *cur_kthread;/* tracks the running kernel context */
56 struct kthread *spare; /* useful when restarting */
57 struct timer_chain tchain; /* for the per-core alarm */
58 unsigned int lock_depth;
59 struct trace_ring traces;
61 uint64_t last_tick_cnt;
62 uint64_t state_ticks[NR_CPU_STATES];
63 /* TODO: 64b (not sure if we'll need these at all */
69 spinlock_t immed_amsg_lock;
70 struct kernel_msg_list immed_amsgs;
71 spinlock_t routine_amsg_lock;
72 struct kernel_msg_list routine_amsgs;
73 /* profiling -- opaque to all but the profiling code. */
75 }__attribute__((aligned(ARCH_CL_SIZE)));
77 /* Allows the kernel to figure out what process is running on this core. Can be
78 * used just like a pointer to a struct proc. */
79 #define current per_cpu_info[core_id()].cur_proc
80 /* Allows the kernel to figure out what *user* ctx is on this core's stack. Can
81 * be used just like a pointer to a struct user_context. Note the distinction
82 * between kernel and user contexts. The kernel always returns to its nested,
83 * interrupted contexts via iret/etc. We never do that for user contexts. */
84 #define current_ctx per_cpu_info[core_id()].cur_ctx
86 typedef struct per_cpu_info per_cpu_info_t;
88 extern per_cpu_info_t per_cpu_info[MAX_NUM_CORES];
89 extern volatile uint32_t num_cores;
91 /* SMP bootup functions */
93 void smp_idle(void) __attribute__((noreturn));
94 void smp_percpu_init(void); // this must be called by each core individually
95 void __arch_pcpu_init(uint32_t coreid); /* each arch has one of these */
97 void __set_cpu_state(struct per_cpu_info *pcpui, int state);
98 void reset_cpu_state_ticks(int coreid);
100 /* SMP utility functions */
101 int smp_call_function_self(isr_t handler, void *data,
102 handler_wrapper_t **wait_wrapper);
103 int smp_call_function_all(isr_t handler, void *data,
104 handler_wrapper_t **wait_wrapper);
105 int smp_call_function_single(uint32_t dest, isr_t handler, void *data,
106 handler_wrapper_t **wait_wrapper);
107 int smp_call_wait(handler_wrapper_t *wrapper);
109 /* PCPUI Trace Rings: */
110 struct pcpu_trace_event {
116 /* If you want to add a type, use the next available number, increment NR_TYPES,
117 * use your own macro, and provide a handler. Add your handler to
118 * pcpui_tr_handlers in smp.c. */
119 #define PCPUI_TR_TYPE_NULL 0
120 #define PCPUI_TR_TYPE_KMSG 1
121 #define PCPUI_TR_TYPE_LOCKS 2
122 #define PCPUI_NR_TYPES 3
124 #ifdef CONFIG_TRACE_KMSGS
126 # define pcpui_trace_kmsg(pcpui, pc) \
128 struct pcpu_trace_event *e = get_trace_slot_racy(&pcpui->traces); \
130 e->type = PCPUI_TR_TYPE_KMSG; \
137 # define pcpui_trace_kmsg(pcpui, pc)
139 #endif /* CONFIG_TRACE_KMSGS */
142 #ifdef CONFIG_TRACE_LOCKS
144 # define pcpui_trace_locks(pcpui, lock) \
146 struct pcpu_trace_event *e = get_trace_slot_overwrite(&pcpui->traces); \
148 e->type = PCPUI_TR_TYPE_LOCKS; \
149 e->arg0 = (int)tsc2usec(read_tsc()); \
150 e->arg1 = (uintptr_t)lock; \
156 # define pcpui_trace_locks(pcpui, lock)
158 #endif /* CONFIG_TRACE_LOCKS */
160 /* Run the handlers for all events in a pcpui ring. Can run on all cores, or
161 * just one core. 'type' selects which event type is handled (0 for all). */
162 void pcpui_tr_foreach(int coreid, int type);
163 void pcpui_tr_foreach_all(int type);
164 void pcpui_tr_reset_all(void);
165 void pcpui_tr_reset_and_clear_all(void);
167 #endif /* ROS_INC_SMP_H */