proc.c

Go to the documentation of this file.
00001 
00040 #include "proc_p.h"
00041 #include "proc.h"
00042 
00043 #include "cfg/cfg_arch.h"  // ARCH_EMUL
00044 #include "cfg/cfg_proc.h"
00045 #include "cfg/cfg_monitor.h"
00046 #include <cfg/macros.h>    // ROUND_UP2
00047 #include <cfg/module.h>
00048 #include <cfg/depend.h>    // CONFIG_DEPEND()
00049 
00050 #include <cpu/irq.h>
00051 #include <cpu/types.h>
00052 #include <cpu/attr.h>
00053 #include <cpu/frame.h>
00054 
00055 #if CONFIG_KERN_HEAP
00056     #include <struct/heap.h>
00057 #endif
00058 
00059 #include <string.h>           /* memset() */
00060 
00061 /*
00062  * The scheduer tracks ready processes by enqueuing them in the
00063  * ready list.
00064  *
00065  * \note Access to the list must occur while interrupts are disabled.
00066  */
00067 REGISTER List ProcReadyList;
00068 
00069 /*
00070  * Holds a pointer to the TCB of the currently running process.
00071  *
00072  * \note User applications should use proc_current() to retrieve this value.
00073  */
00074 REGISTER Process *CurrentProcess;
00075 
00076 #if (ARCH & ARCH_EMUL)
00077 /*
00078  * In some hosted environments, we must emulate the stack on the real
00079  * process stack to satisfy consistency checks in system libraries and
00080  * because some ABIs place trampolines on the stack.
00081  *
00082  * Access to this list must be protected by PROC_ATOMIC().
00083  */
00084 List StackFreeList;
00085 
00086 #define NPROC 10
00087 cpu_stack_t proc_stacks[NPROC][(64 * 1024) / sizeof(cpu_stack_t)];
00088 #endif
00089 
00091 struct Process MainProcess;
00092 
00093 
00094 static void proc_init_struct(Process *proc)
00095 {
00096     /* Avoid warning for unused argument. */
00097     (void)proc;
00098 
00099 #if CONFIG_KERN_SIGNALS
00100     proc->sig_recv = 0;
00101     proc->sig_wait = 0;
00102 #endif
00103 
00104 #if CONFIG_KERN_HEAP
00105     proc->flags = 0;
00106 #endif
00107 
00108 #if CONFIG_KERN_PRI
00109     proc->link.pri = 0;
00110 #endif
00111 
00112 }
00113 
00114 MOD_DEFINE(proc);
00115 
00116 void proc_init(void)
00117 {
00118     LIST_INIT(&ProcReadyList);
00119 
00120 #if ARCH & ARCH_EMUL
00121     LIST_INIT(&StackFreeList);
00122     for (int i = 0; i < NPROC; i++)
00123         ADDTAIL(&StackFreeList, (Node *)proc_stacks[i]);
00124 #endif
00125 
00126     /*
00127      * We "promote" the current context into a real process. The only thing we have
00128      * to do is create a PCB and make it current. We don't need to setup the stack
00129      * pointer because it will be written the first time we switch to another process.
00130      */
00131     proc_init_struct(&MainProcess);
00132     CurrentProcess = &MainProcess;
00133 
00134 #if CONFIG_KERN_MONITOR
00135     monitor_init();
00136     monitor_add(CurrentProcess, "main");
00137 #endif
00138 
00139 #if CONFIG_KERN_PREEMPT
00140     preempt_init();
00141 #endif
00142 
00143     MOD_INIT(proc);
00144 }
00145 
00152 struct Process *proc_new_with_name(UNUSED_ARG(const char *, name), void (*entry)(void), iptr_t data, size_t stack_size, cpu_stack_t *stack_base)
00153 {
00154     Process *proc;
00155     const size_t PROC_SIZE_WORDS = ROUND_UP2(sizeof(Process), sizeof(cpu_stack_t)) / sizeof(cpu_stack_t);
00156 #if CONFIG_KERN_HEAP
00157     bool free_stack = false;
00158 #endif
00159     TRACEMSG("name=%s", name);
00160 
00161 #if (ARCH & ARCH_EMUL)
00162     /* Ignore stack provided by caller and use the large enough default instead. */
00163     PROC_ATOMIC(stack_base = (cpu_stack_t *)list_remHead(&StackFreeList));
00164     ASSERT(stack_base);
00165 
00166     stack_size = CONFIG_KERN_MINSTACKSIZE;
00167 #elif CONFIG_KERN_HEAP
00168     /* Did the caller provide a stack for us? */
00169     if (!stack_base)
00170     {
00171         /* Did the caller specify the desired stack size? */
00172         if (!stack_size)
00173             stack_size = CONFIG_KERN_MINSTACKSIZE;
00174 
00175         /* Allocate stack dinamically */
00176         if (!(stack_base = heap_alloc(stack_size)))
00177             return NULL;
00178 
00179         free_stack = true;
00180     }
00181 
00182 #else // !ARCH_EMUL && !CONFIG_KERN_HEAP
00183 
00184     /* Stack must have been provided by the user */
00185     ASSERT_VALID_PTR(stack_base);
00186     ASSERT(stack_size);
00187 
00188 #endif // !ARCH_EMUL && !CONFIG_KERN_HEAP
00189 
00190 #if CONFIG_KERN_MONITOR
00191     /*
00192      * Fill-in the stack with a special marker to help debugging.
00193      * On 64bit platforms, CONFIG_KERN_STACKFILLCODE is larger
00194      * than an int, so the (int) cast is required to silence the
00195      * warning for truncating its size.
00196      */
00197     memset(stack_base, (int)CONFIG_KERN_STACKFILLCODE, stack_size);
00198 #endif
00199 
00200     /* Initialize the process control block */
00201     if (CPU_STACK_GROWS_UPWARD)
00202     {
00203         proc = (Process *)stack_base;
00204         proc->stack = stack_base + PROC_SIZE_WORDS;
00205         if (CPU_SP_ON_EMPTY_SLOT)
00206             proc->stack++;
00207     }
00208     else
00209     {
00210         proc = (Process *)(stack_base + stack_size / sizeof(cpu_stack_t) - PROC_SIZE_WORDS);
00211         proc->stack = (cpu_stack_t *)proc;
00212         if (CPU_SP_ON_EMPTY_SLOT)
00213             proc->stack--;
00214     }
00215 
00216     proc_init_struct(proc);
00217     proc->user_data = data;
00218 
00219 #if CONFIG_KERN_HEAP | CONFIG_KERN_MONITOR | (ARCH & ARCH_EMUL)
00220     proc->stack_base = stack_base;
00221     proc->stack_size = stack_size;
00222     #if CONFIG_KERN_HEAP
00223     if (free_stack)
00224         proc->flags |= PF_FREESTACK;
00225     #endif
00226 #endif
00227 
00228     #if CONFIG_KERN_PREEMPT
00229 
00230         getcontext(&proc->context);
00231         proc->context.uc_stack.ss_sp = proc->stack;
00232         proc->context.uc_stack.ss_size = stack_size - PROC_SIZE_WORDS - 1;
00233         proc->context.uc_link = NULL;
00234         makecontext(&proc->context, (void (*)(void))proc_entry, 1, entry);
00235 
00236     #else // !CONFIG_KERN_PREEMPT
00237     {
00238         size_t i;
00239 
00240         /* Initialize process stack frame */
00241         CPU_PUSH_CALL_FRAME(proc->stack, proc_exit);
00242         CPU_PUSH_CALL_FRAME(proc->stack, entry);
00243 
00244         /* Push a clean set of CPU registers for asm_switch_context() */
00245         for (i = 0; i < CPU_SAVED_REGS_CNT; i++)
00246             CPU_PUSH_WORD(proc->stack, CPU_REG_INIT_VALUE(i));
00247     }
00248     #endif // CONFIG_KERN_PREEMPT
00249 
00250     #if CONFIG_KERN_MONITOR
00251         monitor_add(proc, name);
00252     #endif
00253 
00254     /* Add to ready list */
00255     ATOMIC(SCHED_ENQUEUE(proc));
00256 
00257     return proc;
00258 }
00259 
00265 const char *proc_name(struct Process *proc)
00266 {
00267     #if CONFIG_KERN_MONITOR
00268         return proc ? proc->monitor.name : "<NULL>";
00269     #else
00270         (void)proc;
00271         return "---";
00272     #endif
00273 }
00274 
00276 const char *proc_currentName(void)
00277 {
00278     return proc_name(proc_current());
00279 }
00280 
00282 void proc_rename(struct Process *proc, const char *name)
00283 {
00284 #if CONFIG_KERN_MONITOR
00285     monitor_rename(proc, name);
00286 #else
00287     (void)proc; (void)name;
00288 #endif
00289 }
00290 
00291 
00292 #if CONFIG_KERN_PRI
00293 
00312 void proc_setPri(struct Process *proc, int pri)
00313 {
00314         if (proc->link.pri == pri)
00315             return;
00316 
00317         proc->link.pri = pri;
00318 
00319         if (proc != CurrentProcess)
00320         {
00321                 //proc_forbid();
00322                 //TODO: re-enqueue process
00323                 //pric_permit();
00324         }
00325 }
00326 #endif // CONFIG_KERN_PRI
00327 
00331 void proc_exit(void)
00332 {
00333     TRACEMSG("%p:%s", CurrentProcess, proc_currentName());
00334 
00335 #if CONFIG_KERN_MONITOR
00336     monitor_remove(CurrentProcess);
00337 #endif
00338 
00339 #if CONFIG_KERN_HEAP
00340     /*
00341      * The following code is BROKEN.
00342      * We are freeing our own stack before entering proc_schedule()
00343      * BAJO: A correct fix would be to rearrange the scheduler with
00344      *  an additional parameter which frees the old stack/process
00345      *  after a context switch.
00346      */
00347     if (CurrentProcess->flags & PF_FREESTACK)
00348         heap_free(CurrentProcess->stack_base, CurrentProcess->stack_size);
00349     heap_free(CurrentProcess);
00350 #endif
00351 
00352 #if (ARCH & ARCH_EMUL)
00353     /* Reinsert process stack in free list */
00354     PROC_ATOMIC(ADDHEAD(&StackFreeList, (Node *)CurrentProcess->stack_base));
00355 
00356     /*
00357      * NOTE: At this point the first two words of what used
00358      * to be our stack contain a list node. From now on, we
00359      * rely on the compiler not reading/writing the stack.
00360      */
00361 #endif /* ARCH_EMUL */
00362 
00363     CurrentProcess = NULL;
00364     proc_switch();
00365     /* not reached */
00366 }
00367 
00368 
00372 iptr_t proc_currentUserData(void)
00373 {
00374     return CurrentProcess->user_data;
00375 }