1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/stage2/tasks.c Sat Apr 23 18:19:38 2016 +0200
1.3 @@ -0,0 +1,92 @@
1.4 +/*
1.5 + * Task handling.
1.6 + *
1.7 + * Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software: you can redistribute it and/or modify
1.10 + * it under the terms of the GNU General Public License as published by
1.11 + * the Free Software Foundation, either version 3 of the License, or
1.12 + * (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.21 + */
1.22 +
1.23 +#include "cpu.h"
1.24 +#include "paging.h"
1.25 +
1.26 +/* Task tables and data. */
1.27 +
1.28 +enum { max_tasks = 3 };
1.29 +static u32 stack_pointers[max_tasks];
1.30 +static u32 registers[max_tasks][32];
1.31 +
1.32 +u8 current_task;
1.33 +u32 *current_stack_pointer;
1.34 +u32 *current_registers;
1.35 +
1.36 +/* Address locations and paging configuration. */
1.37 +
1.38 +const u32 stack_start = 0x00080000;
1.39 +const u32 stack_size = 0x00002000;
1.40 +const u32 pagesize = 4 * 1024;
1.41 +
1.42 +/* The unrelocated symbol table location. */
1.43 +
1.44 +extern u32 _got_copy_start;
1.45 +
1.46 +/* Task management functions. */
1.47 +
1.48 +void init_tasks()
1.49 +{
1.50 + current_task = 0;
1.51 + current_stack_pointer = &stack_pointers[current_task];
1.52 + current_registers = registers[current_task];
1.53 +}
1.54 +
1.55 +void start_task(unsigned short task, void (*function)(), u32 args[], unsigned short nargs)
1.56 +{
1.57 + u32 virtual, physical;
1.58 +
1.59 + /*
1.60 + Each task employs a stack at a multiple of the given start address in
1.61 + physical memory, but at the same address in virtual memory.
1.62 + */
1.63 +
1.64 + virtual = stack_start;
1.65 + physical = stack_start - stack_size * task;
1.66 +
1.67 + init_page_table(page_table_start, virtual - pagesize * 2, physical - pagesize * 2, pagesize, 0x1e, task);
1.68 +
1.69 + /*
1.70 + Subtract from the stack pointer to prevent the called function from
1.71 + reaching into unmapped memory.
1.72 + */
1.73 +
1.74 + stack_pointers[task] = virtual - 12;
1.75 +
1.76 + /*
1.77 + Set the registers for the new task, initialising the global pointer and
1.78 + return address.
1.79 + */
1.80 +
1.81 + init_registers(registers[task], _got_copy_start, function, args, nargs);
1.82 +}
1.83 +
1.84 +void switch_task()
1.85 +{
1.86 + /* Switch the current task. */
1.87 +
1.88 + current_task++;
1.89 + if (current_task == max_tasks) current_task = 0;
1.90 +
1.91 + /* Indicate the current stack pointer and task registers. */
1.92 +
1.93 + current_stack_pointer = &stack_pointers[current_task];
1.94 + current_registers = registers[current_task];
1.95 +}