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@186 | 21 | #include "cpu_op.h" |
paul@193 | 22 | #include "mips.h" |
paul@156 | 23 | #include "paging.h" |
paul@159 | 24 | #include "tasks.h" |
paul@156 | 25 | |
paul@156 | 26 | /* Task tables and data. */ |
paul@156 | 27 | |
paul@156 | 28 | static u32 stack_pointers[max_tasks]; |
paul@156 | 29 | static u32 registers[max_tasks][32]; |
paul@156 | 30 | |
paul@156 | 31 | u8 current_task; |
paul@156 | 32 | u32 *current_stack_pointer; |
paul@156 | 33 | u32 *current_registers; |
paul@156 | 34 | |
paul@156 | 35 | /* Address locations and paging configuration. */ |
paul@156 | 36 | |
paul@193 | 37 | const u32 stack_start = 0x01c00000; |
paul@156 | 38 | const u32 stack_size = 0x00002000; |
paul@156 | 39 | const u32 pagesize = 4 * 1024; |
paul@156 | 40 | |
paul@179 | 41 | /* A reference to locations for the symbol tables. */ |
paul@179 | 42 | |
paul@179 | 43 | extern u32 _got_start, _got_copy_start, _got_copy_end; |
paul@156 | 44 | |
paul@189 | 45 | /* A reference to the start of the payload and end of memory locations. */ |
paul@179 | 46 | |
paul@189 | 47 | extern u32 _payload_start, _memory_end; |
paul@156 | 48 | |
paul@156 | 49 | /* Task management functions. */ |
paul@156 | 50 | |
paul@156 | 51 | void init_tasks() |
paul@156 | 52 | { |
paul@166 | 53 | current_task = 1; |
paul@160 | 54 | init_task(); |
paul@160 | 55 | } |
paul@160 | 56 | |
paul@160 | 57 | void init_task() |
paul@160 | 58 | { |
paul@156 | 59 | current_stack_pointer = &stack_pointers[current_task]; |
paul@156 | 60 | current_registers = registers[current_task]; |
paul@156 | 61 | } |
paul@156 | 62 | |
paul@159 | 63 | void start_task(unsigned short task, void (*function)(), u32 args[], u8 nargs) |
paul@156 | 64 | { |
paul@179 | 65 | u32 virtual, physical, address; |
paul@156 | 66 | |
paul@156 | 67 | /* |
paul@156 | 68 | Each task employs a stack at a multiple of the given start address in |
paul@193 | 69 | physical memory, but at the same address in virtual memory. Task zero |
paul@193 | 70 | is never started. |
paul@156 | 71 | */ |
paul@156 | 72 | |
paul@156 | 73 | virtual = stack_start; |
paul@156 | 74 | physical = stack_start - stack_size * task; |
paul@156 | 75 | |
paul@193 | 76 | init_page_table(page_table_start, virtual - pagesize * 2, physical - pagesize * 2, pagesize, TLB_WRITE, task); |
paul@156 | 77 | |
paul@156 | 78 | /* |
paul@156 | 79 | Subtract from the stack pointer to prevent the called function from |
paul@156 | 80 | reaching into unmapped memory. |
paul@156 | 81 | */ |
paul@156 | 82 | |
paul@156 | 83 | stack_pointers[task] = virtual - 12; |
paul@156 | 84 | |
paul@156 | 85 | /* |
paul@156 | 86 | Set the registers for the new task, initialising the global pointer and |
paul@156 | 87 | return address. |
paul@156 | 88 | */ |
paul@156 | 89 | |
paul@173 | 90 | init_registers(registers[task], (u32) &_got_copy_start, function, args, nargs); |
paul@179 | 91 | |
paul@179 | 92 | /* Map the global object table for the task. */ |
paul@179 | 93 | |
paul@193 | 94 | init_page_table(page_table_start, user_address((u32) &_got_start), user_address((u32) &_got_copy_start), pagesize, TLB_READ, task); |
paul@179 | 95 | |
paul@179 | 96 | /* Map all shared pages for the task. */ |
paul@179 | 97 | |
paul@179 | 98 | for (address = (u32) &_payload_start; address < (u32) &_got_start; address += pagesize * 2) |
paul@179 | 99 | { |
paul@193 | 100 | init_page_table(page_table_start, user_address(address), user_address(address), pagesize, TLB_READ, task); |
paul@179 | 101 | } |
paul@179 | 102 | |
paul@189 | 103 | for (address = (u32) &_got_copy_end + pagesize * 2; address < (u32) &_memory_end; address += pagesize * 2) |
paul@179 | 104 | { |
paul@193 | 105 | init_page_table(page_table_start, user_address(address), user_address(address), pagesize, TLB_WRITE, task); |
paul@179 | 106 | } |
paul@156 | 107 | } |
paul@156 | 108 | |
paul@165 | 109 | void start_task_now() |
paul@165 | 110 | { |
paul@171 | 111 | invoke_task(current_task, current_registers, current_stack_pointer); |
paul@165 | 112 | } |
paul@165 | 113 | |
paul@156 | 114 | void switch_task() |
paul@156 | 115 | { |
paul@156 | 116 | /* Switch the current task. */ |
paul@156 | 117 | |
paul@156 | 118 | current_task++; |
paul@166 | 119 | if (current_task == max_tasks) current_task = 1; |
paul@160 | 120 | init_task(); |
paul@156 | 121 | } |