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@179 | 39 | /* A reference to locations for the symbol tables. */ |
paul@179 | 40 | |
paul@179 | 41 | extern u32 _got_start, _got_copy_start, _got_copy_end; |
paul@156 | 42 | |
paul@179 | 43 | /* A reference to the start of the payload. */ |
paul@179 | 44 | |
paul@179 | 45 | extern u32 _payload_start; |
paul@156 | 46 | |
paul@156 | 47 | /* Task management functions. */ |
paul@156 | 48 | |
paul@156 | 49 | void init_tasks() |
paul@156 | 50 | { |
paul@166 | 51 | current_task = 1; |
paul@160 | 52 | init_task(); |
paul@160 | 53 | } |
paul@160 | 54 | |
paul@160 | 55 | void init_task() |
paul@160 | 56 | { |
paul@156 | 57 | current_stack_pointer = &stack_pointers[current_task]; |
paul@156 | 58 | current_registers = registers[current_task]; |
paul@156 | 59 | } |
paul@156 | 60 | |
paul@159 | 61 | void start_task(unsigned short task, void (*function)(), u32 args[], u8 nargs) |
paul@156 | 62 | { |
paul@179 | 63 | u32 virtual, physical, address; |
paul@156 | 64 | |
paul@156 | 65 | /* |
paul@156 | 66 | Each task employs a stack at a multiple of the given start address in |
paul@156 | 67 | physical memory, but at the same address in virtual memory. |
paul@156 | 68 | */ |
paul@156 | 69 | |
paul@156 | 70 | virtual = stack_start; |
paul@156 | 71 | physical = stack_start - stack_size * task; |
paul@156 | 72 | |
paul@156 | 73 | init_page_table(page_table_start, virtual - pagesize * 2, physical - pagesize * 2, pagesize, 0x1e, task); |
paul@156 | 74 | |
paul@156 | 75 | /* |
paul@156 | 76 | Subtract from the stack pointer to prevent the called function from |
paul@156 | 77 | reaching into unmapped memory. |
paul@156 | 78 | */ |
paul@156 | 79 | |
paul@156 | 80 | stack_pointers[task] = virtual - 12; |
paul@156 | 81 | |
paul@156 | 82 | /* |
paul@156 | 83 | Set the registers for the new task, initialising the global pointer and |
paul@156 | 84 | return address. |
paul@156 | 85 | */ |
paul@156 | 86 | |
paul@173 | 87 | init_registers(registers[task], (u32) &_got_copy_start, function, args, nargs); |
paul@179 | 88 | |
paul@179 | 89 | /* Map the global object table for the task. */ |
paul@179 | 90 | |
paul@179 | 91 | init_page_table(page_table_start, (u32) &_got_start & 0x7fffffff, (u32) &_got_copy_start & 0x7fffffff, pagesize, 0x1e, task); |
paul@179 | 92 | |
paul@179 | 93 | /* Map all shared pages for the task. */ |
paul@179 | 94 | |
paul@179 | 95 | for (address = (u32) &_payload_start; address < (u32) &_got_start; address += pagesize * 2) |
paul@179 | 96 | { |
paul@179 | 97 | init_page_table(page_table_start, address & 0x7fffffff, address & 0x7fffffff, pagesize, 0x1e, task); |
paul@179 | 98 | } |
paul@179 | 99 | |
paul@179 | 100 | for (address = (u32) &_got_copy_end + pagesize * 2; address < 0x82000000; address += pagesize * 2) |
paul@179 | 101 | { |
paul@179 | 102 | init_page_table(page_table_start, address & 0x7fffffff, address & 0x7fffffff, pagesize, 0x1e, task); |
paul@179 | 103 | } |
paul@156 | 104 | } |
paul@156 | 105 | |
paul@165 | 106 | void start_task_now() |
paul@165 | 107 | { |
paul@171 | 108 | invoke_task(current_task, current_registers, current_stack_pointer); |
paul@165 | 109 | } |
paul@165 | 110 | |
paul@156 | 111 | void switch_task() |
paul@156 | 112 | { |
paul@156 | 113 | /* Switch the current task. */ |
paul@156 | 114 | |
paul@156 | 115 | current_task++; |
paul@166 | 116 | if (current_task == max_tasks) current_task = 1; |
paul@160 | 117 | init_task(); |
paul@156 | 118 | } |