2 Copyright © 2015, The AROS Development Team. All rights reserved.
7 #include <exec/alerts.h>
8 #include <exec/execbase.h>
9 #include <exec/lists.h>
10 #include <proto/exec.h>
11 #include <proto/kernel.h>
13 //#include <kernel_base.h>
14 #include <kernel_debug.h>
15 #include <kernel_scheduler.h>
17 #include "kernel_cpu.h"
19 #include <exec_platform.h>
21 #include <aros/types/spinlock_s.h>
25 #include "exec_intern.h"
29 /* Check if the currently running task on this cpu should be rescheduled.. */
30 BOOL
core_Schedule(void)
32 struct Task
*task
= GET_THIS_TASK
;
33 BOOL corereschedule
= TRUE
;
35 DSCHED(bug("[KRN:BCM2708] core_Schedule()\n"));
37 SysBase
->AttnResched
&= ~ARF_AttnSwitch
;
39 /* If task has pending exception, reschedule it so that the dispatcher may handle the exception */
40 if (!(task
->tc_Flags
& TF_EXCEPT
))
42 #if defined(__AROSEXEC_SMP__)
43 KrnSpinLock(&PrivExecBase(SysBase
)->TaskReadySpinLock
,
46 /* Is the TaskReady empty? If yes, then the running task is the only one. Let it work */
47 if (IsListEmpty(&SysBase
->TaskReady
))
48 corereschedule
= FALSE
;
51 struct Task
*nexttask
;
52 #if defined(__AROSEXEC_SMP__)
53 int cpunum
= GetCPUNumber();
54 uint32_t cpumask
= (1 << cpunum
);
57 If there are tasks ready for this cpu that have equal or lower priority,
58 and the current task has used its alloted time - reschedule so they can run
60 for (nexttask
= (struct Task
*)GetHead(&SysBase
->TaskReady
); nexttask
!= NULL
; nexttask
= (struct Task
*)GetSucc(nexttask
))
62 #if defined(__AROSEXEC_SMP__)
63 if ((GetIntETask(nexttask
)->iet_CpuAffinity
& cpumask
) == cpumask
)
66 if (nexttask
->tc_Node
.ln_Pri
<= task
->tc_Node
.ln_Pri
)
68 /* If the running task did not used it's whole quantum yet, let it work */
69 if (!(SysBase
->SysFlags
& SFF_QuantumOver
))
70 corereschedule
= FALSE
;
73 #if defined(__AROSEXEC_SMP__)
78 #if defined(__AROSEXEC_SMP__)
79 KrnSpinUnLock(&PrivExecBase(SysBase
)->TaskReadySpinLock
);
86 bug("[KRN:BCM2708] Setting task 0x%p (%s) to READY\n", task
, task
->tc_Node
.ln_Name
);
89 return corereschedule
;
92 /* Switch the currently running task on this cpu to ready state */
93 void core_Switch(void)
95 struct Task
*task
= GET_THIS_TASK
;
97 DSCHED(bug("[KRN:BCM2708] core_Switch(): Old task = %p (%s)\n", task
, task
->tc_Node
.ln_Name
));
99 #if defined(__AROSEXEC_SMP__)
100 KrnSpinLock(&PrivExecBase(SysBase
)->TaskRunningSpinLock
,
101 SPINLOCK_MODE_WRITE
);
103 Remove(&task
->tc_Node
);
104 #if defined(__AROSEXEC_SMP__)
105 KrnSpinUnLock(&PrivExecBase(SysBase
)->TaskRunningSpinLock
);
108 /* if the current task has gone out of stack bounds, suspend it to prevent further damage to the system */
109 if (task
->tc_SPReg
<= task
->tc_SPLower
|| task
->tc_SPReg
> task
->tc_SPUpper
)
111 bug("[KRN:BCM2708] Task %s went out of stack limits\n", task
->tc_Node
.ln_Name
);
112 bug("[KRN:BCM2708] Lower %p, upper %p, SP %p\n", task
->tc_SPLower
, task
->tc_SPUpper
, task
->tc_SPReg
);
114 task
->tc_SigWait
= 0;
115 task
->tc_State
= TS_WAIT
;
116 #if defined(__AROSEXEC_SMP__)
117 KrnSpinLock(&PrivExecBase(SysBase
)->TaskWaitSpinLock
,
118 SPINLOCK_MODE_WRITE
);
120 Enqueue(&SysBase
->TaskWait
, &task
->tc_Node
);
121 #if defined(__AROSEXEC_SMP__)
122 KrnSpinUnLock(&PrivExecBase(SysBase
)->TaskWaitSpinLock
);
125 Alert(AN_StackProbe
);
127 else if (task
->tc_State
== TS_RUN
)
128 task
->tc_State
= TS_READY
;
130 task
->tc_IDNestCnt
= SysBase
->IDNestCnt
;
132 if (task
->tc_Flags
& TF_SWITCH
)
133 AROS_UFC1NR(void, task
->tc_Switch
, AROS_UFCA(struct ExecBase
*, SysBase
, A6
));
136 /* Dispatch a "new" ready task on this cpu */
137 struct Task
*core_Dispatch(void)
139 struct Task
*newtask
;
140 struct Task
*task
= GET_THIS_TASK
;
141 #if defined(__AROSEXEC_SMP__)
142 int cpunum
= GetCPUNumber();
143 uint32_t cpumask
= (1 << cpunum
);
146 DSCHED(bug("[KRN:BCM2708] core_Dispatch()\n"));
148 #if defined(__AROSEXEC_SMP__)
149 KrnSpinLock(&PrivExecBase(SysBase
)->TaskReadySpinLock
,
150 SPINLOCK_MODE_WRITE
);
152 for (newtask
= (struct Task
*)GetHead(&SysBase
->TaskReady
); newtask
!= NULL
; newtask
= (struct Task
*)GetSucc(newtask
))
154 #if defined(__AROSEXEC_SMP__)
155 if ((GetIntETask(newtask
)->iet_CpuAffinity
& cpumask
) == cpumask
)
158 Remove(&newtask
->tc_Node
);
160 #if defined(__AROSEXEC_SMP__)
165 if ((newtask
== NULL
) && (task
!= NULL
))
168 if ((task
!= NULL
) && (task
->tc_State
== TS_READY
) && (newtask
!= task
))
170 Enqueue(&SysBase
->TaskReady
, &task
->tc_Node
);
172 #if defined(__AROSEXEC_SMP__)
173 KrnSpinUnLock(&PrivExecBase(SysBase
)->TaskReadySpinLock
);
179 SysBase
->DispCount
++;
180 SysBase
->IDNestCnt
= newtask
->tc_IDNestCnt
;
181 SET_THIS_TASK(newtask
);
182 SysBase
->Elapsed
= SysBase
->Quantum
;
183 SysBase
->SysFlags
&= ~SFF_QuantumOver
;
184 newtask
->tc_State
= TS_RUN
;
186 DSCHED(bug("[KRN:BCM2708] New task = %p (%s)\n", newtask
, newtask
->tc_Node
.ln_Name
));
188 /* Check the stack of the task we are about to launch. */
190 if (newtask
->tc_SPReg
<= newtask
->tc_SPLower
|| newtask
->tc_SPReg
> newtask
->tc_SPUpper
)
192 /* Don't let the task run, switch it away (raising Alert) and dispatch another task */
194 return core_Dispatch();
199 /* Is the list of ready tasks empty? Well, go idle. */
200 DSCHED(bug("[KRN:BCM2708] No ready tasks, entering sleep mode\n"));
203 * Idle counter is incremented every time when we enter here,
204 * not only once. This is correct.
206 SysBase
->IdleCount
++;
207 SysBase
->AttnResched
|= ARF_AttnSwitch
;