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