1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Ulf Ralberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
35 # define IF_COP2(x) CURRENT_CORE
38 #define DEADBEEF ((unsigned int)0xdeadbeef)
39 /* Cast to the the machine int type, whose size could be < 4. */
41 struct core_entry cores
[NUM_CORES
] IBSS_ATTR
;
42 struct thread_entry threads
[MAXTHREADS
] IBSS_ATTR
;
43 #ifdef HAVE_SCHEDULER_BOOSTCTRL
44 static int boosted_threads IBSS_ATTR
;
47 /* Define to enable additional checks for blocking violations etc. */
48 #define THREAD_EXTRA_CHECKS
50 static const char main_thread_name
[] = "main";
52 extern int stackbegin
[];
53 extern int stackend
[];
57 extern int cop_stackbegin
[];
58 extern int cop_stackend
[];
60 /* The coprocessor stack is not set up in the bootloader code, but the threading
61 * is. No threads are run on the coprocessor, so set up some dummy stack */
62 int *cop_stackbegin
= stackbegin
;
63 int *cop_stackend
= stackend
;
69 static long cores_locked IBSS_ATTR
;
71 #define LOCK(...) do { } while (test_and_set(&cores_locked, 1))
72 #define UNLOCK(...) cores_locked = 0
75 /* #warning "Core locking mechanism should be fixed on H10/4G!" */
77 inline void lock_cores(void)
80 if (!cores
[CURRENT_CORE
].lock_issued
)
83 cores
[CURRENT_CORE
].lock_issued
= true;
88 inline void unlock_cores(void)
91 if (cores
[CURRENT_CORE
].lock_issued
)
93 cores
[CURRENT_CORE
].lock_issued
= false;
102 static void add_to_list(struct thread_entry **list,
103 struct thread_entry *thread) ICODE_ATTR;
104 static void remove_from_list(struct thread_entry **list,
105 struct thread_entry *thread) ICODE_ATTR;
108 void switch_thread(bool save_context
, struct thread_entry
**blocked_list
)
111 static inline void store_context(void* addr
) __attribute__ ((always_inline
));
112 static inline void load_context(const void* addr
)
113 __attribute__ ((always_inline
));
114 static inline void core_sleep(void) __attribute__((always_inline
));
117 /*---------------------------------------------------------------------------
118 * Store non-volatile context.
119 *---------------------------------------------------------------------------
121 static inline void store_context(void* addr
)
124 "stmia %0, { r4-r11, sp, lr }\n"
129 /*---------------------------------------------------------------------------
130 * Load non-volatile context.
131 *---------------------------------------------------------------------------
133 static void start_thread(void (*thread_func
)(void), const void* addr
) __attribute__((naked
,used
));
134 static void start_thread(void (*thread_func
)(void), const void* addr
)
136 /* r0 = thread_func, r1 = addr */
137 #if NUM_CORES > 1 && CONFIG_CPU != PP5002
140 "str r2, [r1, #40] \n"
141 "ldr r1, =0xf000f044 \n" /* invalidate this core's cache */
145 "ldr r1, =0x6000c000 \n"
156 "str r2, [r1, #40] \n"
165 static inline void load_context(const void* addr
)
168 "ldmia %0, { r4-r11, sp, lr } \n" /* load regs r4 to r14 from context */
169 "ldr r0, [%0, #40] \n" /* load start pointer */
170 "cmp r0, #0 \n" /* check for NULL */
171 "movne r1, %0 \n" /* if not already running, jump to start */
172 "ldrne pc, =start_thread \n"
173 : : "r" (addr
) : "r0", "r1"
178 static inline void core_sleep(void)
182 /* This should sleep the CPU. It appears to wake by itself on
184 if (CURRENT_CORE
== CPU
)
185 CPU_CTL
= PROC_SLEEP
;
187 COP_CTL
= PROC_SLEEP
;
191 #elif CONFIG_CPU == S3C2440
192 static inline void core_sleep(void)
195 CLKCON
|= (1 << 2); /* set IDLE bit */
196 for(i
=0; i
<10; i
++); /* wait for IDLE */
197 CLKCON
&= ~(1 << 2); /* reset IDLE bit when wake up */
201 #elif defined(CPU_COLDFIRE)
202 /*---------------------------------------------------------------------------
203 * Store non-volatile context.
204 *---------------------------------------------------------------------------
206 static inline void store_context(void* addr
)
209 "move.l %%macsr,%%d0 \n"
210 "movem.l %%d0/%%d2-%%d7/%%a2-%%a7,(%0) \n"
211 : : "a" (addr
) : "d0" /* only! */
215 /*---------------------------------------------------------------------------
216 * Load non-volatile context.
217 *---------------------------------------------------------------------------
219 static inline void load_context(const void* addr
)
222 "movem.l (%0),%%d0/%%d2-%%d7/%%a2-%%a7 \n" /* Load context */
223 "move.l %%d0,%%macsr \n"
224 "move.l (52,%0),%%d0 \n" /* Get start address */
225 "beq.b 1f \n" /* NULL -> already running */
226 "clr.l (52,%0) \n" /* Clear start address.. */
228 "jmp (%0) \n" /* ..and start the thread */
230 : : "a" (addr
) : "d0" /* only! */
234 static inline void core_sleep(void)
236 asm volatile ("stop #0x2000");
239 /* Set EMAC unit to fractional mode with saturation for each new thread,
240 since that's what'll be the most useful for most things which the dsp
241 will do. Codecs should still initialize their preferred modes
243 #define THREAD_CPU_INIT(core, thread) \
244 ({ (thread)->context.macsr = EMAC_FRACTIONAL | EMAC_SATURATE; })
246 #elif CONFIG_CPU == SH7034
247 /*---------------------------------------------------------------------------
248 * Store non-volatile context.
249 *---------------------------------------------------------------------------
251 static inline void store_context(void* addr
)
268 /*---------------------------------------------------------------------------
269 * Load non-volatile context.
270 *---------------------------------------------------------------------------
272 static inline void load_context(const void* addr
)
284 "mov.l @%0,r0 \n" /* Get start address */
286 "bt .running \n" /* NULL -> already running */
289 "rts \n" /* Start the thread */
290 "mov.l r0,@%0 \n" /* Clear start address */
292 : : "r" (addr
) : "r0" /* only! */
296 static inline void core_sleep(void)
299 asm volatile ("sleep");
304 #ifndef THREAD_CPU_INIT
305 /* No cpu specific init - make empty */
306 #define THREAD_CPU_INIT(core, thread)
309 #ifdef THREAD_EXTRA_CHECKS
310 static void thread_panicf_format_name(char *buffer
, struct thread_entry
*thread
)
315 /* Display thread name if one or ID if none */
316 const char *fmt
= thread
->name
? " %s" : " %08lX";
317 intptr_t name
= thread
->name
?
318 (intptr_t)thread
->name
: (intptr_t)thread
;
319 snprintf(buffer
, 16, fmt
, name
);
323 static void thread_panicf(const char *msg
,
324 struct thread_entry
*thread1
, struct thread_entry
*thread2
)
326 static char thread1_name
[16], thread2_name
[16];
327 thread_panicf_format_name(thread1_name
, thread1
);
328 thread_panicf_format_name(thread2_name
, thread2
);
329 panicf ("%s%s%s", msg
, thread1_name
, thread2_name
);
332 static void thread_stkov(void)
334 /* Display thread name if one or ID if none */
335 struct thread_entry
*current
= cores
[CURRENT_CORE
].running
;
336 const char *fmt
= current
->name
? "%s %s" : "%s %08lX";
337 intptr_t name
= current
->name
?
338 (intptr_t)current
->name
: (intptr_t)current
;
339 panicf(fmt
, "Stkov", name
);
341 #endif /* THREAD_EXTRA_CHECKS */
343 static void add_to_list(struct thread_entry
**list
, struct thread_entry
*thread
)
347 thread
->next
= thread
;
348 thread
->prev
= thread
;
354 thread
->next
= *list
;
355 thread
->prev
= (*list
)->prev
;
356 thread
->prev
->next
= thread
;
357 (*list
)->prev
= thread
;
360 thread->next = (*list)->next;
361 thread->prev = *list;
362 thread->next->prev = thread;
363 (*list)->next = thread;
368 static void remove_from_list(struct thread_entry
**list
,
369 struct thread_entry
*thread
)
373 if (thread
== thread
->next
)
380 *list
= thread
->next
;
383 /* Fix links to jump over the removed entry. */
384 thread
->prev
->next
= thread
->next
;
385 thread
->next
->prev
= thread
->prev
;
388 static void check_sleepers(void) __attribute__ ((noinline
));
389 static void check_sleepers(void)
391 struct thread_entry
*current
, *next
;
393 /* Check sleeping threads. */
394 current
= cores
[CURRENT_CORE
].sleeping
;
400 next
= current
->next
;
402 if ((unsigned)current_tick
>= GET_STATE_ARG(current
->statearg
))
404 /* Sleep timeout has been reached so bring the thread
405 * back to life again. */
406 remove_from_list(&cores
[CURRENT_CORE
].sleeping
, current
);
407 add_to_list(&cores
[CURRENT_CORE
].running
, current
);
408 current
->statearg
= 0;
410 /* If there is no more processes in the list, break the loop. */
411 if (cores
[CURRENT_CORE
].sleeping
== NULL
)
420 /* Break the loop once we have walked through the list of all
421 * sleeping processes. */
422 if (current
== cores
[CURRENT_CORE
].sleeping
)
427 /* Safely finish waking all threads potentialy woken by interrupts -
428 * statearg already zeroed in wakeup_thread. */
429 static void wake_list_awaken(void) __attribute__ ((noinline
));
430 static void wake_list_awaken(void)
432 int oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
434 /* No need for another check in the IRQ lock since IRQs are allowed
435 only to add threads to the waking list. They won't be adding more
436 until we're done here though. */
438 struct thread_entry
*waking
= cores
[CURRENT_CORE
].waking
;
439 struct thread_entry
*running
= cores
[CURRENT_CORE
].running
;
443 /* Place waking threads at the end of the running list. */
444 struct thread_entry
*tmp
;
445 waking
->prev
->next
= running
;
446 running
->prev
->next
= waking
;
448 running
->prev
= waking
->prev
;
453 /* Just transfer the list as-is - just came out of a core
455 cores
[CURRENT_CORE
].running
= waking
;
458 /* Done with waking list */
459 cores
[CURRENT_CORE
].waking
= NULL
;
460 set_irq_level(oldlevel
);
463 static inline void sleep_core(void)
467 /* We want to do these ASAP as it may change the decision to sleep
468 the core or the core has woken because an interrupt occurred
469 and posted a message to a queue. */
470 if (cores
[CURRENT_CORE
].waking
!= NULL
)
473 if (cores
[CURRENT_CORE
].last_tick
!= current_tick
)
476 cores
[CURRENT_CORE
].last_tick
= current_tick
;
479 /* We must sleep until there is at least one process in the list
480 * of running processes. */
481 if (cores
[CURRENT_CORE
].running
!= NULL
)
484 /* Enter sleep mode to reduce power usage, woken up on interrupt */
490 static int get_threadnum(struct thread_entry
*thread
)
494 for (i
= 0; i
< MAXTHREADS
; i
++)
496 if (&threads
[i
] == thread
)
503 void profile_thread(void) {
504 profstart(get_threadnum(cores
[CURRENT_CORE
].running
));
508 static void change_thread_state(struct thread_entry
**blocked_list
) __attribute__ ((noinline
));
509 static void change_thread_state(struct thread_entry
**blocked_list
)
511 struct thread_entry
*old
;
512 unsigned long new_state
;
514 /* Remove the thread from the list of running threads. */
515 old
= cores
[CURRENT_CORE
].running
;
516 new_state
= GET_STATE(old
->statearg
);
518 /* Check if a thread state change has been requested. */
521 /* Change running thread state and switch to next thread. */
522 remove_from_list(&cores
[CURRENT_CORE
].running
, old
);
524 /* And put the thread into a new list of inactive threads. */
525 if (new_state
== STATE_BLOCKED
)
526 add_to_list(blocked_list
, old
);
528 add_to_list(&cores
[CURRENT_CORE
].sleeping
, old
);
530 #ifdef HAVE_PRIORITY_SCHEDULING
531 /* Reset priorities */
532 if (old
->priority
== cores
[CURRENT_CORE
].highest_priority
)
533 cores
[CURRENT_CORE
].highest_priority
= 100;
537 /* Switch to the next running thread. */
538 cores
[CURRENT_CORE
].running
= old
->next
;
541 /*---------------------------------------------------------------------------
542 * Switch thread in round robin fashion.
543 *---------------------------------------------------------------------------
545 void switch_thread(bool save_context
, struct thread_entry
**blocked_list
)
548 profile_thread_stopped(get_threadnum(cores
[CURRENT_CORE
].running
));
550 unsigned int *stackptr
;
558 /* Begin task switching by saving our current context so that we can
559 * restore the state of the current thread later to the point prior
563 store_context(&cores
[CURRENT_CORE
].running
->context
);
565 /* Check if the current thread stack is overflown */
566 stackptr
= cores
[CURRENT_CORE
].running
->stack
;
567 if(stackptr
[0] != DEADBEEF
)
568 #ifdef THREAD_EXTRA_CHECKS
569 thread_panicf("Stkov", cores
[CURRENT_CORE
].running
, NULL
);
574 /* Rearrange thread lists as needed */
575 change_thread_state(blocked_list
);
577 /* This has to be done after the scheduler is finished with the
578 blocked_list pointer so that an IRQ can't kill us by attempting
579 a wake but before attempting any core sleep. */
580 if (cores
[CURRENT_CORE
].switch_to_irq_level
!= STAY_IRQ_LEVEL
)
582 int level
= cores
[CURRENT_CORE
].switch_to_irq_level
;
583 cores
[CURRENT_CORE
].switch_to_irq_level
= STAY_IRQ_LEVEL
;
584 set_irq_level(level
);
588 /* Go through the list of sleeping task to check if we need to wake up
589 * any of them due to timeout. Also puts core into sleep state until
590 * there is at least one running process again. */
593 #ifdef HAVE_PRIORITY_SCHEDULING
594 /* Select the new task based on priorities and the last time a process
598 int priority
= cores
[CURRENT_CORE
].running
->priority
;
600 if (priority
< cores
[CURRENT_CORE
].highest_priority
)
601 cores
[CURRENT_CORE
].highest_priority
= priority
;
603 if (priority
== cores
[CURRENT_CORE
].highest_priority
||
604 (current_tick
- cores
[CURRENT_CORE
].running
->last_run
>
606 cores
[CURRENT_CORE
].running
->priority_x
!= 0)
611 cores
[CURRENT_CORE
].running
= cores
[CURRENT_CORE
].running
->next
;
614 /* Reset the value of thread's last running time to the current time. */
615 cores
[CURRENT_CORE
].running
->last_run
= current_tick
;
621 /* And finally give control to the next thread. */
622 load_context(&cores
[CURRENT_CORE
].running
->context
);
625 profile_thread_started(get_threadnum(cores
[CURRENT_CORE
].running
));
629 void sleep_thread(int ticks
)
631 struct thread_entry
*current
;
635 current
= cores
[CURRENT_CORE
].running
;
637 #ifdef HAVE_SCHEDULER_BOOSTCTRL
638 if (STATE_IS_BOOSTED(current
->statearg
))
641 if (!boosted_threads
)
648 /* Set the thread's new state and timeout and finally force a task switch
649 * so that scheduler removes thread from the list of running processes
650 * and puts it in list of sleeping tasks. */
651 SET_STATE(current
->statearg
, STATE_SLEEPING
, current_tick
+ ticks
+ 1);
653 switch_thread(true, NULL
);
656 void block_thread(struct thread_entry
**list
)
658 struct thread_entry
*current
;
662 /* Get the entry for the current running thread. */
663 current
= cores
[CURRENT_CORE
].running
;
665 #ifdef HAVE_SCHEDULER_BOOSTCTRL
666 /* Keep the boosted state over indefinite block calls, because
667 * we are waiting until the earliest time that someone else
668 * completes an action */
669 unsigned long boost_flag
= STATE_IS_BOOSTED(current
->statearg
);
672 #ifdef THREAD_EXTRA_CHECKS
673 /* We are not allowed to mix blocking types in one queue. */
674 if (*list
&& GET_STATE((*list
)->statearg
) == STATE_BLOCKED_W_TMO
)
675 thread_panicf("Blocking violation B->*T", current
, *list
);
678 /* Set the state to blocked and ask the scheduler to switch tasks,
679 * this takes us off of the run queue until we are explicitly woken */
680 SET_STATE(current
->statearg
, STATE_BLOCKED
, 0);
682 switch_thread(true, list
);
684 #ifdef HAVE_SCHEDULER_BOOSTCTRL
685 /* Reset only the boosted flag to indicate we are up and running again. */
686 current
->statearg
= boost_flag
;
688 /* Clear all flags to indicate we are up and running again. */
689 current
->statearg
= 0;
693 void block_thread_w_tmo(struct thread_entry
**list
, int timeout
)
695 struct thread_entry
*current
;
696 /* Get the entry for the current running thread. */
697 current
= cores
[CURRENT_CORE
].running
;
700 #ifdef HAVE_SCHEDULER_BOOSTCTRL
701 /* A block with a timeout is a sleep situation, whatever we are waiting
702 * for _may or may not_ happen, regardless of boost state, (user input
703 * for instance), so this thread no longer needs to boost */
704 if (STATE_IS_BOOSTED(current
->statearg
))
707 if (!boosted_threads
)
714 #ifdef THREAD_EXTRA_CHECKS
715 /* We can store only one thread to the "list" if thread is used
716 * in other list (such as core's list for sleeping tasks). */
718 thread_panicf("Blocking violation T->*B", current
, NULL
);
721 /* Set the state to blocked with the specified timeout */
722 SET_STATE(current
->statearg
, STATE_BLOCKED_W_TMO
, current_tick
+ timeout
);
724 /* Set the "list" for explicit wakeup */
727 /* Now force a task switch and block until we have been woken up
728 * by another thread or timeout is reached. */
729 switch_thread(true, NULL
);
731 /* It is now safe for another thread to block on this "list" */
735 #if !defined(SIMULATOR)
736 void set_irq_level_and_block_thread(struct thread_entry
**list
, int level
)
738 cores
[CURRENT_CORE
].switch_to_irq_level
= level
;
742 void set_irq_level_and_block_thread_w_tmo(struct thread_entry
**list
,
743 int timeout
, int level
)
745 cores
[CURRENT_CORE
].switch_to_irq_level
= level
;
746 block_thread_w_tmo(list
, timeout
);
750 void wakeup_thread(struct thread_entry
**list
)
752 struct thread_entry
*thread
;
754 /* Check if there is a blocked thread at all. */
760 /* Wake up the last thread first. */
763 /* Determine thread's current state. */
764 switch (GET_STATE(thread
->statearg
))
767 /* Remove thread from the list of blocked threads and add it
768 * to the scheduler's list of running processes. List removal
769 * is safe since each object maintains it's own list of
770 * sleepers and queues protect against reentrancy. */
771 remove_from_list(list
, thread
);
772 add_to_list(cores
[IF_COP2(thread
->core
)].wakeup_list
, thread
);
774 case STATE_BLOCKED_W_TMO
:
775 /* Just remove the timeout to cause scheduler to immediately
776 * wake up the thread. */
777 thread
->statearg
= 0;
781 /* Nothing to do. Thread has already been woken up
782 * or it's state is not blocked or blocked with timeout. */
787 inline static int find_empty_thread_slot(void)
791 for (n
= 0; n
< MAXTHREADS
; n
++)
793 if (threads
[n
].name
== NULL
)
800 /* Like wakeup_thread but safe against IRQ corruption when IRQs are disabled
802 void wakeup_thread_irq_safe(struct thread_entry
**list
)
804 struct core_entry
*core
= &cores
[CURRENT_CORE
];
805 /* Switch wakeup lists and call wakeup_thread */
806 core
->wakeup_list
= &core
->waking
;
808 /* Switch back to normal running list */
809 core
->wakeup_list
= &core
->running
;
812 /*---------------------------------------------------------------------------
814 * If using a dual core architecture, specify which core to start the thread
815 * on, and whether to fall back to the other core if it can't be created
816 * Return ID if context area could be allocated, else NULL.
817 *---------------------------------------------------------------------------
820 create_thread(void (*function
)(void), void* stack
, int stack_size
,
821 const char *name
IF_PRIO(, int priority
)
822 IF_COP(, unsigned int core
, bool fallback
))
825 unsigned int stacklen
;
826 unsigned int *stackptr
;
829 struct thread_entry
*thread
;
833 * To prevent ifdef hell while keeping the binary size down, we define
834 * core here if it hasn't been passed as a parameter
841 /* If the kernel hasn't initialised on the COP (most likely due to an old
842 * bootloader) then refuse to start threads on the COP
844 if ((core
== COP
) && !cores
[core
].kernel_running
)
847 return create_thread(function
, stack
, stack_size
, name
848 IF_PRIO(, priority
) IF_COP(, CPU
, false));
856 slot
= find_empty_thread_slot();
863 /* Munge the stack to make it easy to spot stack overflows */
864 stacklen
= stack_size
/ sizeof(int);
866 for(i
= 0;i
< stacklen
;i
++)
868 stackptr
[i
] = DEADBEEF
;
871 /* Store interesting information */
872 thread
= &threads
[slot
];
874 thread
->stack
= stack
;
875 thread
->stack_size
= stack_size
;
876 thread
->statearg
= 0;
877 #ifdef HAVE_PRIORITY_SCHEDULING
878 thread
->priority_x
= 0;
879 thread
->priority
= priority
;
880 cores
[core
].highest_priority
= 100;
887 regs
= &thread
->context
;
888 /* Align stack to an even 32 bit boundary */
889 regs
->sp
= (void*)(((unsigned int)stack
+ stack_size
) & ~3);
890 regs
->start
= (void*)function
;
892 /* Do any CPU specific inits after initializing common items
893 to have access to valid data */
894 THREAD_CPU_INIT(core
, thread
);
896 add_to_list(&cores
[core
].running
, thread
);
905 #ifdef HAVE_SCHEDULER_BOOSTCTRL
906 void trigger_cpu_boost(void)
910 if (!STATE_IS_BOOSTED(cores
[CURRENT_CORE
].running
->statearg
))
912 SET_BOOST_STATE(cores
[CURRENT_CORE
].running
->statearg
);
913 if (!boosted_threads
)
924 /*---------------------------------------------------------------------------
925 * Remove a thread on the current core from the scheduler.
926 * Parameter is the ID as returned from create_thread().
927 *---------------------------------------------------------------------------
929 void remove_thread(struct thread_entry
*thread
)
934 thread
= cores
[CURRENT_CORE
].running
;
936 /* Free the entry by removing thread name. */
938 #ifdef HAVE_PRIORITY_SCHEDULING
939 cores
[IF_COP2(thread
->core
)].highest_priority
= 100;
942 if (thread
== cores
[IF_COP2(thread
->core
)].running
)
944 remove_from_list(&cores
[IF_COP2(thread
->core
)].running
, thread
);
945 switch_thread(false, NULL
);
949 if (thread
== cores
[IF_COP2(thread
->core
)].sleeping
)
950 remove_from_list(&cores
[IF_COP2(thread
->core
)].sleeping
, thread
);
952 remove_from_list(NULL
, thread
);
957 #ifdef HAVE_PRIORITY_SCHEDULING
958 int thread_set_priority(struct thread_entry
*thread
, int priority
)
964 thread
= cores
[CURRENT_CORE
].running
;
966 old_priority
= thread
->priority
;
967 thread
->priority
= priority
;
968 cores
[IF_COP2(thread
->core
)].highest_priority
= 100;
974 int thread_get_priority(struct thread_entry
*thread
)
977 thread
= cores
[CURRENT_CORE
].running
;
979 return thread
->priority
;
982 void priority_yield(void)
984 struct thread_entry
*thread
= cores
[CURRENT_CORE
].running
;
985 thread
->priority_x
= 1;
986 switch_thread(true, NULL
);
987 thread
->priority_x
= 0;
989 #endif /* HAVE_PRIORITY_SCHEDULING */
991 struct thread_entry
* thread_get_current(void)
993 return cores
[CURRENT_CORE
].running
;
996 void init_threads(void)
998 unsigned int core
= CURRENT_CORE
;
1001 /* Let main CPU initialize first. */
1005 while (!cores
[CPU
].kernel_running
) ;
1010 slot
= find_empty_thread_slot();
1012 cores
[core
].sleeping
= NULL
;
1013 cores
[core
].running
= NULL
;
1014 cores
[core
].waking
= NULL
;
1015 cores
[core
].wakeup_list
= &cores
[core
].running
;
1016 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
1017 cores
[core
].switch_to_irq_level
= STAY_IRQ_LEVEL
;
1019 threads
[slot
].name
= main_thread_name
;
1020 threads
[slot
].statearg
= 0;
1021 threads
[slot
].context
.start
= 0; /* core's main thread already running */
1023 threads
[slot
].core
= core
;
1025 #ifdef HAVE_PRIORITY_SCHEDULING
1026 threads
[slot
].priority
= PRIORITY_USER_INTERFACE
;
1027 threads
[slot
].priority_x
= 0;
1028 cores
[core
].highest_priority
= 100;
1030 #ifdef HAVE_SCHEDULER_BOOSTCTRL
1031 boosted_threads
= 0;
1033 add_to_list(&cores
[core
].running
, &threads
[slot
]);
1035 /* In multiple core setups, each core has a different stack. There is
1036 * probably a much better way to do this. */
1039 threads
[slot
].stack
= stackbegin
;
1040 threads
[slot
].stack_size
= (int)stackend
- (int)stackbegin
;
1042 #if NUM_CORES > 1 /* This code path will not be run on single core targets */
1045 threads
[slot
].stack
= cop_stackbegin
;
1046 threads
[slot
].stack_size
=
1047 (int)cop_stackend
- (int)cop_stackbegin
;
1050 cores
[core
].kernel_running
= true;
1056 int thread_stack_usage(const struct thread_entry
*thread
)
1059 unsigned int *stackptr
= thread
->stack
;
1061 for (i
= 0;i
< thread
->stack_size
/sizeof(int);i
++)
1063 if (stackptr
[i
] != DEADBEEF
)
1067 return ((thread
->stack_size
- i
* sizeof(int)) * 100) /
1071 int thread_get_status(const struct thread_entry
*thread
)
1073 return GET_STATE(thread
->statearg
);