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