paul@156 | 1 | /* |
paul@156 | 2 | * Task handling. |
paul@156 | 3 | * |
paul@156 | 4 | * Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@156 | 5 | * |
paul@156 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@156 | 7 | * it under the terms of the GNU General Public License as published by |
paul@156 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@156 | 9 | * (at your option) any later version. |
paul@156 | 10 | * |
paul@156 | 11 | * This program is distributed in the hope that it will be useful, |
paul@156 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@156 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@156 | 14 | * GNU General Public License for more details. |
paul@156 | 15 | * |
paul@156 | 16 | * You should have received a copy of the GNU General Public License |
paul@156 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@156 | 18 | */ |
paul@156 | 19 | |
paul@156 | 20 | #include "cpu.h" |
paul@156 | 21 | #include "paging.h" |
paul@159 | 22 | #include "tasks.h" |
paul@156 | 23 | |
paul@156 | 24 | /* Task tables and data. */ |
paul@156 | 25 | |
paul@156 | 26 | static u32 stack_pointers[max_tasks]; |
paul@156 | 27 | static u32 registers[max_tasks][32]; |
paul@156 | 28 | |
paul@156 | 29 | u8 current_task; |
paul@156 | 30 | u32 *current_stack_pointer; |
paul@156 | 31 | u32 *current_registers; |
paul@156 | 32 | |
paul@156 | 33 | /* Address locations and paging configuration. */ |
paul@156 | 34 | |
paul@156 | 35 | const u32 stack_start = 0x00080000; |
paul@156 | 36 | const u32 stack_size = 0x00002000; |
paul@156 | 37 | const u32 pagesize = 4 * 1024; |
paul@156 | 38 | |
paul@159 | 39 | /* A reference to the unrelocated symbol table location. */ |
paul@156 | 40 | |
paul@156 | 41 | extern u32 _got_copy_start; |
paul@156 | 42 | |
paul@156 | 43 | /* Task management functions. */ |
paul@156 | 44 | |
paul@156 | 45 | void init_tasks() |
paul@156 | 46 | { |
paul@166 | 47 | current_task = 1; |
paul@160 | 48 | init_task(); |
paul@160 | 49 | } |
paul@160 | 50 | |
paul@160 | 51 | void init_task() |
paul@160 | 52 | { |
paul@156 | 53 | current_stack_pointer = &stack_pointers[current_task]; |
paul@156 | 54 | current_registers = registers[current_task]; |
paul@156 | 55 | } |
paul@156 | 56 | |
paul@159 | 57 | void start_task(unsigned short task, void (*function)(), u32 args[], u8 nargs) |
paul@156 | 58 | { |
paul@156 | 59 | u32 virtual, physical; |
paul@156 | 60 | |
paul@156 | 61 | /* |
paul@156 | 62 | Each task employs a stack at a multiple of the given start address in |
paul@156 | 63 | physical memory, but at the same address in virtual memory. |
paul@156 | 64 | */ |
paul@156 | 65 | |
paul@156 | 66 | virtual = stack_start; |
paul@156 | 67 | physical = stack_start - stack_size * task; |
paul@156 | 68 | |
paul@156 | 69 | init_page_table(page_table_start, virtual - pagesize * 2, physical - pagesize * 2, pagesize, 0x1e, task); |
paul@156 | 70 | |
paul@156 | 71 | /* |
paul@156 | 72 | Subtract from the stack pointer to prevent the called function from |
paul@156 | 73 | reaching into unmapped memory. |
paul@156 | 74 | */ |
paul@156 | 75 | |
paul@156 | 76 | stack_pointers[task] = virtual - 12; |
paul@156 | 77 | |
paul@156 | 78 | /* |
paul@156 | 79 | Set the registers for the new task, initialising the global pointer and |
paul@156 | 80 | return address. |
paul@156 | 81 | */ |
paul@156 | 82 | |
paul@156 | 83 | init_registers(registers[task], _got_copy_start, function, args, nargs); |
paul@156 | 84 | } |
paul@156 | 85 | |
paul@165 | 86 | void start_task_now() |
paul@165 | 87 | { |
paul@165 | 88 | invoke_task(current_registers); |
paul@165 | 89 | } |
paul@165 | 90 | |
paul@156 | 91 | void switch_task() |
paul@156 | 92 | { |
paul@156 | 93 | /* Switch the current task. */ |
paul@156 | 94 | |
paul@156 | 95 | current_task++; |
paul@166 | 96 | if (current_task == max_tasks) current_task = 1; |
paul@160 | 97 | init_task(); |
paul@156 | 98 | } |