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