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>
31 uintptr_t stacktop; /* must be first */
32 int coreid; /* must be second */
33 /* virtual machines */
34 /* this is all kind of gross, but so it goes. Kmalloc
35 * the vmxarea. It varies in size depending on the architecture.
39 pseudodesc_t host_gdt;
44 /* Process management */
45 // cur_proc should be valid on all cores that are not management cores.
46 struct proc *cur_proc; /* which process context is loaded */
47 struct proc *owning_proc; /* proc owning the core / cur_ctx */
48 uint32_t owning_vcoreid; /* vcoreid of owning proc (if applicable */
49 struct user_context *cur_ctx; /* user ctx we came in on (can be 0) */
50 struct user_context actual_ctx; /* storage for cur_ctx */
51 uint32_t __ctx_depth; /* don't access directly. see trap.h. */
52 int __lock_checking_enabled;/* == 1, enables spinlock depth checking */
53 struct kthread *cur_kthread;/* tracks the running kernel context */
54 struct kthread *spare; /* useful when restarting */
55 struct timer_chain tchain; /* for the per-core alarm */
56 unsigned int lock_depth;
57 struct trace_ring traces;
60 // held spin-locks. this will have to go elsewhere if multiple kernel
61 // threads can share a CPU.
62 // zra: Used by Ivy. Let me know if this should go elsewhere.
63 sharC_env_t sharC_env;
65 /* TODO: 64b (not sure if we'll need these at all */
71 spinlock_t immed_amsg_lock;
72 struct kernel_msg_list NTPTV(a0t) NTPTV(a1t) NTPTV(a2t) immed_amsgs;
73 spinlock_t routine_amsg_lock;
74 struct kernel_msg_list NTPTV(a0t) NTPTV(a1t) NTPTV(a2t) routine_amsgs;
75 /* profiling -- opaque to all but the profiling code. */
77 }__attribute__((aligned(ARCH_CL_SIZE)));
79 /* Allows the kernel to figure out what process is running on this core. Can be
80 * used just like a pointer to a struct proc. */
81 #define current per_cpu_info[core_id()].cur_proc
82 /* Allows the kernel to figure out what *user* ctx is on this core's stack. Can
83 * be used just like a pointer to a struct user_context. Note the distinction
84 * between kernel and user contexts. The kernel always returns to its nested,
85 * interrupted contexts via iret/etc. We never do that for user contexts. */
86 #define current_ctx per_cpu_info[core_id()].cur_ctx
88 typedef struct per_cpu_info NTPTV(t) NTPTV(a0t) NTPTV(a1t) NTPTV(a2t) per_cpu_info_t;
90 extern per_cpu_info_t (RO per_cpu_info)[MAX_NUM_CPUS];
91 extern volatile uint32_t RO num_cpus;
93 /* SMP bootup functions */
95 void smp_idle(void) __attribute__((noreturn));
96 void smp_percpu_init(void); // this must be called by each core individually
97 void __arch_pcpu_init(uint32_t coreid); /* each arch has one of these */
99 /* SMP utility functions */
100 int smp_call_function_self(isr_t handler, void *data,
101 handler_wrapper_t **wait_wrapper);
102 int smp_call_function_all(isr_t handler, void *data,
103 handler_wrapper_t **wait_wrapper);
104 int smp_call_function_single(uint32_t dest, isr_t handler, void *data,
105 handler_wrapper_t **wait_wrapper);
106 int smp_call_wait(handler_wrapper_t *wrapper);
108 /* PCPUI Trace Rings: */
109 struct pcpu_trace_event {
115 /* If you want to add a type, use the next available number, increment NR_TYPES,
116 * use your own macro, and provide a handler. Add your handler to
117 * pcpui_tr_handlers in smp.c. */
118 #define PCPUI_TR_TYPE_NULL 0
119 #define PCPUI_TR_TYPE_KMSG 1
120 #define PCPUI_TR_TYPE_LOCKS 2
121 #define PCPUI_NR_TYPES 3
123 #ifdef CONFIG_TRACE_KMSGS
125 # define pcpui_trace_kmsg(pcpui, pc) \
127 struct pcpu_trace_event *e = get_trace_slot_racy(&pcpui->traces); \
129 e->type = PCPUI_TR_TYPE_KMSG; \
136 # define pcpui_trace_kmsg(pcpui, pc)
138 #endif /* CONFIG_TRACE_KMSGS */
141 #ifdef CONFIG_TRACE_LOCKS
143 # define pcpui_trace_locks(pcpui, lock) \
145 struct pcpu_trace_event *e = get_trace_slot_overwrite(&pcpui->traces); \
147 e->type = PCPUI_TR_TYPE_LOCKS; \
148 e->arg0 = (int)tsc2usec(read_tsc()); \
149 e->arg1 = (uintptr_t)lock; \
155 # define pcpui_trace_locks(pcpui, lock)
157 #endif /* CONFIG_TRACE_LOCKS */
159 /* Run the handlers for all events in a pcpui ring. Can run on all cores, or
160 * just one core. 'type' selects which event type is handled (0 for all). */
161 void pcpui_tr_foreach(int coreid, int type);
162 void pcpui_tr_foreach_all(int type);
163 void pcpui_tr_reset_all(void);
164 void pcpui_tr_reset_and_clear_all(void);
166 #endif /* ROS_INC_SMP_H */