Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 976cf66

Browse files
committed
working generic scheduler handling code
from to user user user sys user svc sys sys sys user sys svc svc user svc svc svc sys
1 parent af7f6d6 commit 976cf66

File tree

8 files changed

+60
-32
lines changed

8 files changed

+60
-32
lines changed

arch/exception.S

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@ undefined_vec:
3232

3333

3434
swi_vec:
35-
stmfd sp!,{r0-r12,lr}
35+
stmfd sp,{r0-r14}^
36+
sub sp, sp, #60
37+
stmfd sp!, {lr}
3638
# read the swi instruction
3739
ldr r0, [lr, #-4]
3840
# mask of the top 8 bits
3941
bic r0, r0, #0xff000000
4042
mov lr,pc
4143
ldr pc,=syscall
42-
ldmfd sp!,{r0-r12,pc}^
44+
ldmfd sp!, {lr}
45+
ldmfd sp, {r0-r14}^
46+
add sp, sp, #60
47+
movs pc, lr
4348

4449
abort_data_vec:
4550
b abort_data_vec

arch/switch.S

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ save_process_context_in_irq:
3131
b restore_process_regs
3232

3333
save_process_context_sys:
34-
mov pc, lr
34+
mov r0, lr
35+
msr cpsr_c, #IRQ_MD
36+
ldr sp, =IRQ_STACK
37+
mov lr, r0
38+
stmfd sp!, {r0-r12, lr}
39+
b save_process_context_in_irq
3540

3641
save_process_context_svc:
3742
mov r0, sp
@@ -42,7 +47,7 @@ save_process_context_svc:
4247
mov r0, lr
4348
mrs r1, cpsr
4449
mrs r2, spsr
45-
stmia sp!, {r0-r3}
50+
stmia sp!, {r0-r2}
4651
4752
ldr sp, =SVC_STACK
4853
b restore_process_regs
@@ -72,9 +77,8 @@ restore_process_svc_regs:
7277
msr spsr, r5
7378
ldr r13, =nxt_pcb_ptr
7479
ldr sp, [r13]
75-
ldmia sp!, {r0-r12}
76-
ldmia sp!, {r0,r14}
77-
mov sp, r0
80+
ldmia sp, {r0-r14}
81+
/* mov sp, r0*/
7882
mov pc, lr /* we are already in svc mode, do not use movs, it will change mode */
7983

8084

@@ -119,14 +123,5 @@ restore_process_sys_regs:
119123

120124

121125
122-
123-
124-
.global cur_pcb_ptr, nxt_pcb_ptr
125-
126-
.data
127-
cur_pcb_ptr:
128-
.word 0x00000000
129-
nxt_pcb_ptr:
130-
.word 0x00000000
131126
outage_string:
132127
.string "You moron, screwed up everything"

include/support.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef __SUPPORT_H
22
#define __SUPPORT_H
33
#define log_info_str(str,...) \
4-
printk("%20s:%20s:%d:%s: "str"\n", __FILE__, __func__, __LINE__,get_cpsr_info(),##__VA_ARGS__);
4+
printk("%15s:%15s:%d:%s: "str"\n", __FILE__, __func__, __LINE__,get_cpsr_info(),##__VA_ARGS__);
55
// printk(str, ##__VA_ARGS__);
66
#define log_info() printk("%s:%s:%d: %s\n", __FILE__, __func__, __LINE__, get_cpsr_info())
77

kernel/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void mem_init();
2121
void scheduler_init();
2222
int idle_thread();
2323
int normal_thread();
24+
int normal_thread1();
2425

2526
#define QEMU_CMDLINE_ADDR 0x12c
2627

@@ -50,6 +51,8 @@ int main()
5051

5152

5253
create_thread(normal_thread, "normal_thread", 0x110);
54+
create_thread(normal_thread, "normal_thread1", 0x110);
55+
create_thread(normal_thread1, "screw_up", 0x11f);
5356

5457

5558
__asm__ __volatile__("msr cpsr_c, #0x1f");

kernel/process.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,47 @@
44
void dump_regs();
55
int idle_thread()
66
{
7+
// set_stack_top(get_current()->usr_stack_top);
8+
__asm__ __volatile__("mov sp, %0"::"r"(get_usr_stack()));
79
printk("In idle thread: %d\n", get_pid());
8-
int i=0;
10+
int i=100;
11+
int stop;
912
while (1) {
1013
sleep(1000, get_pid());
11-
log_info_str("Running %s: %d", get_task_name(), i++);
12-
dump_regs();
14+
__asm__ __volatile("mov %0, sp" :"=r"(stop)::);
15+
log_info_str("Running %s: %d, stop = %x", get_task_name(), i++, stop);
16+
// dump_regs();
1317
}
1418
return 0;
1519
}
1620
int normal_thread()
1721
{
1822
printk("Normal thread %d\n", get_pid());
19-
int i = 0;
23+
int i = 20;
24+
int stop = 0;
2025
while (1) {
2126
sleep(1000, get_pid());
22-
dump_regs();
23-
log_info_str("Running %s: pid = %d,%d", get_task_name(), get_pid(), i++);
27+
// dump_regs();
28+
__asm__ __volatile("mov %0, sp" :"=r"(stop)::);
29+
log_info_str("Running %s: pid = %d,%d, stop = %x", get_task_name(), get_pid(), i++, stop);
2430
__asm__ __volatile__("swi #20"::);
25-
log_info_str("back to user space id = %d\n", get_pid());
26-
dump_regs();
31+
log_info_str("back to user space %s: pid = %d,%d, stop = %x", get_task_name(), get_pid(), i++, stop);
32+
// dump_regs();
33+
}
34+
return 0;
35+
}
36+
37+
int normal_thread1()
38+
{
39+
printk("Normal thread1 %d\n", get_pid());
40+
int i = 200;
41+
int stop = 0;
42+
while (1) {
43+
__asm__ __volatile("mov %0, sp" :"=r"(stop)::);
44+
log_info_str("before schedule %s: pid = %d,%d, stop = %x", get_task_name(), get_pid(), i++, stop);
45+
schedule();
46+
log_info_str("after schedule %s: pid = %d,%d, stop = %x", get_task_name(), get_pid(), i++, stop);
47+
// dump_regs();
2748
}
2849
return 0;
2950
}

kernel/sched.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
*/
1414

1515
//extern int context_switch_req;
16+
#if 0
1617
extern int cur_pcb_ptr, nxt_pcb_ptr;
18+
#endif
19+
pcontext *cur_pcb_ptr, *nxt_pcb_ptr;
1720
static int sched_needed;
1821

1922
extern void context_switch();
@@ -71,8 +74,8 @@ void schedule()
7174

7275
printk("***********Context switch ************\n");
7376
unset_schedule_needed();
74-
cur_pcb_ptr = (int)get_current();
75-
nxt_pcb_ptr = (int)(get_current()->next);
77+
cur_pcb_ptr = get_current();
78+
nxt_pcb_ptr = (get_current()->next);
7679

7780
switch (get_cur_mode()) {
7881

kernel/syscall.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ static long syscall_table[MAX_SYSCALL+1]= {0};
2222
void syscall(int no)
2323
{
2424
log_info_str("In syscall: %d before schedule", no);
25-
dump_regs();
2625
schedule();
27-
dump_regs();
28-
//log_info_str("In syscall: %d after schedule", no);
26+
log_info_str("In syscall: %d after schedule", no);
2927
}
3028

3129
void __div0(void)

kernel/task.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,13 @@ char *get_task_name()
7878
return get_current()->name;
7979

8080
}
81-
81+
int get_usr_stack()
82+
{
83+
return (long)((get_current())->usr_stack_top);
84+
}
8285
int get_svc_stack()
8386
{
84-
return (get_current())->svc_stack_top;
87+
return (long)((get_current())->svc_stack_top);
8588
}
8689

8790
pcontext *common_thread_create(int pid, int (*thread_fn)(), const char *name, unsigned int mode)

0 commit comments

Comments
 (0)