Skip to content

Commit

Permalink
Improved context saving.
Browse files Browse the repository at this point in the history
  • Loading branch information
pykello committed Aug 22, 2014
1 parent 1bd7ba5 commit 57f9334
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
13 changes: 13 additions & 0 deletions include/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
#ifndef PROC_H
#define PROC_H

#ifdef __ASSEMBLER__

#define SAVE_CONTEXT \
push {r0, r14};\
mov r0, lr;\
bl save_context;\
pop {r0, r14};

#else

#include <vm.h>
#include <types.h>

Expand Down Expand Up @@ -90,6 +100,8 @@ void proc_expand_memory(struct Process *proc, int page_count);
void proc_shrink_memory(struct Process *proc, int page_count);
bool proc_load(struct Process *proc, char **proc_image, int page_count);
void proc_start(struct Process *proc);
void set_current_process(struct Process *proc);
int *get_current_context(void);

/* scheduler.c */
void scheduler_init(void);
Expand All @@ -105,3 +117,4 @@ int syscall_fork(void);
int syscall_exec(int id);

#endif
#endif
1 change: 1 addition & 0 deletions kernel/proc/build.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
PROC_DIR = kernel/proc

OBJS += $(PROC_DIR)/proc.o
OBJS += $(PROC_DIR)/save_context.o
OBJS += $(PROC_DIR)/scheduler.o
OBJS += $(PROC_DIR)/syscall_exit.o
OBJS += $(PROC_DIR)/syscall_getpid.o
Expand Down
7 changes: 5 additions & 2 deletions kernel/proc/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ __attribute__((__aligned__(SECTION_TABLE_ALIGNMENT)))
struct SectionTableEntry process_vm[PROCESS_COUNT_MAX][4096];

struct Process *current_process = NULL;
int *current_context;

/* proc_init initializes the process sub-system. */
void proc_init(void)
Expand Down Expand Up @@ -176,7 +175,6 @@ bool proc_load(struct Process *proc, char **proc_image, int page_count)
void proc_start(struct Process *proc)
{
current_process = proc;
current_context = proc->context;

set_translation_table_base((uint32_t) V2P(proc->vm));

Expand All @@ -186,3 +184,8 @@ void proc_start(struct Process *proc)

switch_to_context(proc->context);
}

int *get_current_context(void)
{
return current_process->context;
}
34 changes: 34 additions & 0 deletions kernel/proc/save_context.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

.global save_context
save_context:
push {r0-r12, r14}

/* return address of process is passed in r0 */
mov r14, r0

/* load address of context array for current process into r0 */
push {r1-r12, r14}
bl get_current_context
pop {r1-r12, r14}

/* if no process is active, return */
cmpeq r0, #0
beq save_context_return

/* store cpsr */
push {r1}
mrs r1, spsr
str r1, [r0]
pop {r1}

/* store return address */
add r0, #4
str r14, [r0]

/* store the rest */
add r0, #8
stmia r0, {r1-r14}^

save_context_return:
pop {r0-r12, r14}
mov pc, lr
26 changes: 5 additions & 21 deletions kernel/startup.S
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <vm.h>
#include <proc.h>

.global entry
entry:
Expand Down Expand Up @@ -67,35 +68,18 @@ interrupt_table_start:
interrupt_table_end:

syscall_entry:
stmfd r13!, {r1-r12, r14}

push {r0-r12, r14}

ldr r0, current_context_address
ldr r0, [r0]

push {r1}
mrs r1, spsr
str r1, [r0]
pop {r1}

add r0, #4
str r14, [r0]
SAVE_CONTEXT

add r0, #8
stmia r0, {r1-r14}^

pop {r0-r12, r14}
stmfd r13!, {r1-r12, r14}

bl handle_syscall

ldmfd r13!, {r1-r12, pc}^


current_context_address: .word current_context

irq_entry:
sub r14, r14, #4
SAVE_CONTEXT

stmfd r13!, {r0-r12, r14}

bl dispatch_interrupts
Expand Down

0 comments on commit 57f9334

Please sign in to comment.