proc.h

Go to the documentation of this file.
00001 
00092 #ifndef KERN_PROC_H
00093 #define KERN_PROC_H
00094 
00095 #include "cfg/cfg_proc.h"
00096 #include "cfg/cfg_signal.h"
00097 #include "cfg/cfg_monitor.h"
00098 #include "sem.h"
00099 
00100 #include <struct/list.h> // Node, PriNode
00101 
00102 #include <cfg/compiler.h>
00103 #include <cfg/debug.h> // ASSERT()
00104 
00105 #include <cpu/types.h> // cpu_stack_t
00106 #include <cpu/frame.h> // CPU_SAVED_REGS_CNT
00107 
00108 /* The following silents warnings on nightly tests. We need to regenerate
00109  * all the projects before this can be removed.
00110  */
00111 #ifndef CONFIG_KERN_PRI_INHERIT
00112 #define CONFIG_KERN_PRI_INHERIT 0
00113 #endif
00114 
00115 /*
00116  * WARNING: struct Process is considered private, so its definition can change any time
00117  * without notice. DO NOT RELY on any field defined here, use only the interface
00118  * functions below.
00119  *
00120  * You have been warned.
00121  */
00122 typedef struct Process
00123 {
00124 #if CONFIG_KERN_PRI
00125     PriNode      link;        
00126 # if CONFIG_KERN_PRI_INHERIT
00127     PriNode      inh_link;    
00128     List         inh_list;    
00129     Semaphore    *inh_blocked_by;  
00130     int          orig_pri;    
00131 # endif
00132 #else
00133     Node         link;        
00134 #endif
00135     cpu_stack_t  *stack;       
00136     iptr_t       user_data;   
00138 #if CONFIG_KERN_SIGNALS
00139     Signal       sig;
00140 #endif
00141 
00142 #if CONFIG_KERN_HEAP
00143     uint16_t     flags;       
00144 #endif
00145 
00146 #if CONFIG_KERN_HEAP | CONFIG_KERN_MONITOR
00147     cpu_stack_t  *stack_base;  
00148     size_t       stack_size;  
00149 #endif
00150 
00151     /* The actual process entry point */
00152     void (*user_entry)(void);
00153 
00154 #if CONFIG_KERN_MONITOR
00155     struct ProcMonitor
00156     {
00157         Node        link;
00158         const char *name;
00159     } monitor;
00160 #endif
00161 
00162 } Process;
00163 
00168 void proc_init(void);
00169 
00170 struct Process *proc_new_with_name(const char *name, void (*entry)(void), iptr_t data, size_t stacksize, cpu_stack_t *stack);
00171 
00172 #if !CONFIG_KERN_MONITOR
00173 
00192     #define proc_new(entry,data,size,stack) proc_new_with_name(NULL,(entry),(data),(size),(stack))
00193 #else
00194     #define proc_new(entry,data,size,stack) proc_new_with_name(#entry,(entry),(data),(size),(stack))
00195 #endif
00196 
00200 void proc_exit(void);
00201 
00202 /*
00203  * Public scheduling class methods.
00204  */
00205 void proc_yield(void);
00206 
00207 #if CONFIG_KERN_PREEMPT
00208 bool proc_needPreempt(void);
00209 void proc_preempt(void);
00210 #else
00211 INLINE bool proc_needPreempt(void)
00212 {
00213     return false;
00214 }
00215 
00216 INLINE void proc_preempt(void)
00217 {
00218 }
00219 #endif
00220 
00221 void proc_rename(struct Process *proc, const char *name);
00222 const char *proc_name(struct Process *proc);
00223 const char *proc_currentName(void);
00224 
00232 INLINE iptr_t proc_currentUserData(void)
00233 {
00234     extern struct Process *current_process;
00235     return current_process->user_data;
00236 }
00237 
00238 int proc_testSetup(void);
00239 int proc_testRun(void);
00240 int proc_testTearDown(void);
00241 
00249 INLINE struct Process *proc_current(void)
00250 {
00251     extern struct Process *current_process;
00252     return current_process;
00253 }
00254 
00255 #if CONFIG_KERN_PRI
00256     void proc_setPri(struct Process *proc, int pri);
00257 #else
00258     INLINE void proc_setPri(UNUSED_ARG(struct Process *,proc), UNUSED_ARG(int, pri))
00259     {
00260     }
00261 #endif
00262 
00263 #if CONFIG_KERN_PREEMPT
00264 
00282     INLINE void proc_forbid(void)
00283     {
00284         extern cpu_atomic_t preempt_count;
00285         /*
00286          * We don't need to protect the counter against other processes.
00287          * The reason why is a bit subtle.
00288          *
00289          * If a process gets here, preempt_forbid_cnt can be either 0,
00290          * or != 0.  In the latter case, preemption is already disabled
00291          * and no concurrency issues can occur.
00292          *
00293          * In the former case, we could be preempted just after reading the
00294          * value 0 from memory, and a concurrent process might, in fact,
00295          * bump the value of preempt_forbid_cnt under our nose!
00296          *
00297          * BUT: if this ever happens, then we won't get another chance to
00298          * run until the other process calls proc_permit() to re-enable
00299          * preemption.  At this point, the value of preempt_forbid_cnt
00300          * must be back to 0, and thus what we had originally read from
00301          * memory happens to be valid.
00302          *
00303          * No matter how hard you think about it, and how complicated you
00304          * make your scenario, the above holds true as long as
00305          * "preempt_forbid_cnt != 0" means that no task switching is
00306          * possible.
00307          */
00308         ++preempt_count;
00309 
00310         /*
00311          * Make sure preempt_count is flushed to memory so the preemption
00312          * softirq will see the correct value from now on.
00313          */
00314         MEMORY_BARRIER;
00315     }
00316 
00322     INLINE void proc_permit(void)
00323     {
00324         extern cpu_atomic_t preempt_count;
00325 
00326         /*
00327          * This is to ensure any global state changed by the process gets
00328          * flushed to memory before task switching is re-enabled.
00329          */
00330         MEMORY_BARRIER;
00331         /* No need to protect against interrupts here. */
00332         ASSERT(preempt_count > 0);
00333         --preempt_count;
00334         /*
00335          * This ensures preempt_count is flushed to memory immediately so the
00336          * preemption interrupt sees the correct value.
00337          */
00338         MEMORY_BARRIER;
00339     }
00340 
00346     INLINE bool proc_preemptAllowed(void)
00347     {
00348         extern cpu_atomic_t preempt_count;
00349         return (preempt_count == 0);
00350     }
00351 #else /* CONFIG_KERN_PREEMPT */
00352     #define proc_forbid() /* NOP */
00353     #define proc_permit() /* NOP */
00354     #define proc_preemptAllowed() (true)
00355 #endif /* CONFIG_KERN_PREEMPT */
00356 
00358 #define proc_allowed() proc_preemptAllowed()
00359 
00363 #define PROC_ATOMIC(CODE) \
00364     do { \
00365         proc_forbid(); \
00366         CODE; \
00367         proc_permit(); \
00368     } while(0)
00369 
00388 #if (ARCH & ARCH_EMUL)
00389     /* We need a large stack because system libraries are bloated */
00390     #define KERN_MINSTACKSIZE 65536
00391 #else
00392     #if CONFIG_KERN_PREEMPT
00393         /*
00394          * A preemptible kernel needs a larger stack compared to the
00395          * cooperative case. A task can be interrupted anytime in each
00396          * node of the call graph, at any level of depth. This may
00397          * result in a higher stack consumption, to call the ISR, save
00398          * the current user context and to execute the kernel
00399          * preemption routines implemented as ISR prologue and
00400          * epilogue. All these calls are nested into the process stack.
00401          *
00402          * So, to reduce the risk of stack overflow/underflow problems
00403          * add a x2 to the portion stack reserved to the user process.
00404          */
00405         #define KERN_MINSTACKSIZE \
00406             (sizeof(Process) + CPU_SAVED_REGS_CNT * 2 * sizeof(cpu_stack_t) \
00407             + 32 * sizeof(int) * 2)
00408     #else
00409         #define KERN_MINSTACKSIZE \
00410             (sizeof(Process) + CPU_SAVED_REGS_CNT * 2 * sizeof(cpu_stack_t) \
00411             + 32 * sizeof(int))
00412     #endif /* CONFIG_KERN_PREEMPT */
00413 
00414 #endif
00415 
00416 #ifndef CONFIG_KERN_MINSTACKSIZE
00417     /* For backward compatibility */
00418     #define CONFIG_KERN_MINSTACKSIZE KERN_MINSTACKSIZE
00419 #else
00420     #warning FIXME: This macro is deprecated, use KERN_MINSTACKSIZE instead
00421 #endif
00422 
00434 #define PROC_DEFINE_STACK(name, size) \
00435     cpu_stack_t name[((size) + sizeof(cpu_stack_t) - 1) / sizeof(cpu_stack_t)]; \
00436     STATIC_ASSERT((size) >= KERN_MINSTACKSIZE);
00437 
00438 /* Memory fill codes to help debugging */
00439 #if CONFIG_KERN_MONITOR
00440     #include <cpu/types.h>
00441     #if (SIZEOF_CPUSTACK_T == 1)
00442         /* 8bit cpu_stack_t */
00443         #define CONFIG_KERN_STACKFILLCODE  0xA5
00444         #define CONFIG_KERN_MEMFILLCODE    0xDB
00445     #elif (SIZEOF_CPUSTACK_T == 2)
00446         /* 16bit cpu_stack_t */
00447         #define CONFIG_KERN_STACKFILLCODE  0xA5A5
00448         #define CONFIG_KERN_MEMFILLCODE    0xDBDB
00449     #elif (SIZEOF_CPUSTACK_T == 4)
00450         /* 32bit cpu_stack_t */
00451         #define CONFIG_KERN_STACKFILLCODE  0xA5A5A5A5UL
00452         #define CONFIG_KERN_MEMFILLCODE    0xDBDBDBDBUL
00453     #elif (SIZEOF_CPUSTACK_T == 8)
00454         /* 64bit cpu_stack_t */
00455         #define CONFIG_KERN_STACKFILLCODE  0xA5A5A5A5A5A5A5A5ULL
00456         #define CONFIG_KERN_MEMFILLCODE    0xDBDBDBDBDBDBDBDBULL
00457     #else
00458         #error No cpu_stack_t size supported!
00459     #endif
00460 #endif
00461  //defgroup kern_proc
00462 
00463 #endif /* KERN_PROC_H */