Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/proc/thread.c

    rc0699467 rf22dc820  
    173173#endif /* CONFIG_FPU */
    174174       
     175        /*
     176         * Allocate the kernel stack from the low-memory to prevent an infinite
     177         * nesting of TLB-misses when accessing the stack from the part of the
     178         * TLB-miss handler written in C.
     179         *
     180         * Note that low-memory is safe to be used for the stack as it will be
     181         * covered by the kernel identity mapping, which guarantees not to
     182         * nest TLB-misses infinitely (either via some hardware mechanism or
     183         * by the construciton of the assembly-language part of the TLB-miss
     184         * handler).
     185         *
     186         * This restriction can be lifted once each architecture provides
     187         * a similar guarantee, for example by locking the kernel stack
     188         * in the TLB whenever it is allocated from the high-memory and the
     189         * thread is being scheduled to run.
     190         */
     191        kmflags |= FRAME_LOWMEM;
     192        kmflags &= ~FRAME_HIGHMEM;
     193       
    175194        thread->kstack = (uint8_t *) frame_alloc(STACK_FRAMES, FRAME_KA | kmflags);
    176195        if (!thread->kstack) {
     
    217236       
    218237        atomic_set(&nrdy, 0);
    219         thread_slab = slab_cache_create("thread_slab", sizeof(thread_t), 0,
     238        thread_slab = slab_cache_create("thread_t", sizeof(thread_t), 0,
    220239            thr_constructor, thr_destructor, 0);
    221240       
    222241#ifdef CONFIG_FPU
    223         fpu_context_slab = slab_cache_create("fpu_slab", sizeof(fpu_context_t),
    224             FPU_CONTEXT_ALIGN, NULL, NULL, 0);
     242        fpu_context_slab = slab_cache_create("fpu_context_t",
     243            sizeof(fpu_context_t), FPU_CONTEXT_ALIGN, NULL, NULL, 0);
    225244#endif
    226245       
     
    228247}
    229248
     249/** Wire thread to the given CPU
     250 *
     251 * @param cpu CPU to wire the thread to.
     252 *
     253 */
     254void thread_wire(thread_t *thread, cpu_t *cpu)
     255{
     256        irq_spinlock_lock(&thread->lock, true);
     257        thread->cpu = cpu;
     258        thread->wired = true;
     259        irq_spinlock_unlock(&thread->lock, true);
     260}
     261
    230262/** Make thread ready
    231263 *
     
    241273        ASSERT(thread->state != Ready);
    242274       
    243         int i = (thread->priority < RQ_COUNT - 1)
    244             ? ++thread->priority : thread->priority;
    245        
    246         cpu_t *cpu = CPU;
    247         if (thread->flags & THREAD_FLAG_WIRED) {
     275        int i = (thread->priority < RQ_COUNT - 1) ?
     276            ++thread->priority : thread->priority;
     277       
     278        cpu_t *cpu;
     279        if (thread->wired || thread->nomigrate || thread->fpu_context_engaged) {
    248280                ASSERT(thread->cpu != NULL);
    249281                cpu = thread->cpu;
    250         }
     282        } else
     283                cpu = CPU;
     284       
    251285        thread->state = Ready;
    252286       
     
    279313 * @param flags     Thread flags.
    280314 * @param name      Symbolic name (a copy is made).
    281  * @param uncounted Thread's accounting doesn't affect accumulated task
    282  *                  accounting.
    283315 *
    284316 * @return New thread's structure on success, NULL on failure.
     
    286318 */
    287319thread_t *thread_create(void (* func)(void *), void *arg, task_t *task,
    288     unsigned int flags, const char *name, bool uncounted)
     320    thread_flags_t flags, const char *name)
    289321{
    290322        thread_t *thread = (thread_t *) slab_alloc(thread_slab, 0);
     
    316348        thread->ucycles = 0;
    317349        thread->kcycles = 0;
    318         thread->uncounted = uncounted;
     350        thread->uncounted =
     351            ((flags & THREAD_FLAG_UNCOUNTED) == THREAD_FLAG_UNCOUNTED);
    319352        thread->priority = -1;          /* Start in rq[0] */
    320353        thread->cpu = NULL;
    321         thread->flags = flags;
     354        thread->wired = false;
     355        thread->stolen = false;
     356        thread->uspace =
     357            ((flags & THREAD_FLAG_USPACE) == THREAD_FLAG_USPACE);
     358       
    322359        thread->nomigrate = 0;
    323360        thread->state = Entering;
     
    337374        thread->task = task;
    338375       
    339         thread->fpu_context_exists = 0;
    340         thread->fpu_context_engaged = 0;
     376        thread->fpu_context_exists = false;
     377        thread->fpu_context_engaged = false;
    341378       
    342379        avltree_node_initialize(&thread->threads_tree_node);
     
    352389        thread_create_arch(thread);
    353390       
    354         if (!(flags & THREAD_FLAG_NOATTACH))
     391        if ((flags & THREAD_FLAG_NOATTACH) != THREAD_FLAG_NOATTACH)
    355392                thread_attach(thread, task);
    356393       
     
    418455       
    419456        /* Must not count kbox thread into lifecount */
    420         if (thread->flags & THREAD_FLAG_USPACE)
     457        if (thread->uspace)
    421458                atomic_inc(&task->lifecount);
    422459       
     
    440477void thread_exit(void)
    441478{
    442         if (THREAD->flags & THREAD_FLAG_USPACE) {
     479        if (THREAD->uspace) {
    443480#ifdef CONFIG_UDEBUG
    444481                /* Generate udebug THREAD_E event */
    445482                udebug_thread_e_event();
    446 
     483               
    447484                /*
    448485                 * This thread will not execute any code or system calls from
     
    487524{
    488525        ASSERT(THREAD);
    489 
     526       
    490527        THREAD->nomigrate++;
    491528}
     
    496533        ASSERT(THREAD);
    497534        ASSERT(THREAD->nomigrate > 0);
    498 
    499         THREAD->nomigrate--;
     535       
     536        if (THREAD->nomigrate > 0)
     537                THREAD->nomigrate--;
    500538}
    501539
     
    835873         * In case of failure, kernel_uarg will be deallocated in this function.
    836874         * In case of success, kernel_uarg will be freed in uinit().
    837          *
    838875         */
    839876        uspace_arg_t *kernel_uarg =
     
    847884       
    848885        thread_t *thread = thread_create(uinit, kernel_uarg, TASK,
    849             THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf, false);
     886            THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf);
    850887        if (thread) {
    851888                if (uspace_thread_id != NULL) {
Note: See TracChangeset for help on using the changeset viewer.