1 /* 2 * A system pager implementation residing in a separate task. 3 * 4 * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (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, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/env.h> 23 #include <l4/sys/task.h> 24 #include <l4/util/util.h> 25 26 #include <ipc/cap_alloc.h> 27 #include <ipc/mem_ipc.h> 28 #include <mem/memory_utils.h> 29 #include <systypes/base.h> 30 31 #include <stdio.h> 32 33 #include "external_pager.h" 34 #include "parent_pager_object_server.h" 35 36 37 38 #define DEBUG 0 39 40 41 42 /* A simple system pager also acting as a region mapper. */ 43 44 ExternalPager::ExternalPager(address_t start, address_t end) 45 : ExecPager(start, end), NotificationSupport() 46 { 47 } 48 49 ipc_server_default_config_type ExternalPager::config() 50 { 51 return config_ParentPagerObject; 52 } 53 54 55 56 /* Close the pager. */ 57 58 void ExternalPager::close() 59 { 60 printf("Pager closing...\n"); 61 62 /* Unmap all regions. */ 63 64 MappedRegions::iterator it; 65 66 for (it = _regions.begin(); it != _regions.end(); it++) 67 { 68 MappedRegion &r = it->second; 69 70 ipc_detach_dataspace((void *) r.ds_start); 71 } 72 } 73 74 75 76 /* Manage the task and thread capabilities. */ 77 78 void ExternalPager::set_task(l4_cap_idx_t cap) 79 { 80 _task = cap; 81 } 82 83 void ExternalPager::set_server(l4_cap_idx_t cap) 84 { 85 _server = cap; 86 } 87 88 void ExternalPager::set_gate(l4_cap_idx_t cap) 89 { 90 _ipc_gate = cap; 91 } 92 93 94 95 /* Handle a general exception. */ 96 97 long ExternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) 98 { 99 (void) region; 100 101 printf("ExternalPager::exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); 102 103 #if DEBUG 104 while (1) 105 l4_sleep_forever(); 106 #endif 107 108 return L4_EOK; 109 } 110 111 /* Handle a page fault using any configured regions. */ 112 113 long ExternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) 114 { 115 l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; 116 117 #if DEBUG 118 printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); 119 #endif 120 121 /* Find the first region whose start address is beyond the fault address, 122 testing if any immediately preceding region contains the fault address. */ 123 124 MappedRegions::iterator it = _regions.upper_bound(addr); 125 126 if (it != _regions.begin()) 127 it--; 128 else 129 { 130 printf("not mapped at %lx for pc %lx\n", addr, pc); 131 return -L4_ENOMEM; 132 } 133 134 /* Obtain the region and test if it contains the fault address. */ 135 136 MappedRegion &r = it->second; 137 138 if ((addr >= r.start) && (addr < r.start + r.size)) 139 { 140 l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); 141 map_flags_t map_flags = map_flags_for_fault(flags); 142 143 region->fpage = l4_fpage(r.ds_start + (page_addr - r.start), L4_PAGESHIFT, map_flags & r.flags); 144 region->snd_base = page_addr; 145 146 #if DEBUG 147 printf("%lx...%lx from %lx...%lx offset %lx size %d rights %x ds %lx\n", 148 r.start, region->snd_base, 149 r.ds_start, l4_fpage_memaddr(region->fpage), 150 addr - r.start, 151 l4_fpage_size(region->fpage), 152 l4_fpage_rights(region->fpage), 153 r.ds); 154 155 printf("%lx -> ", addr); 156 157 for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) 158 printf("%02x", *((unsigned char *)(r.ds_start + (addr - r.start) + i))); 159 160 printf("\n"); 161 #endif 162 163 if (r.flags & L4RE_RM_F_W) 164 l4_touch_rw((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); 165 else 166 l4_touch_ro((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); 167 168 return L4_EOK; 169 } 170 171 #if DEBUG 172 printf("not mapped!\n"); 173 #endif 174 175 return -L4_ENOMEM; 176 } 177 178 /* Attach a region for provision when page faults occur. This is required in 179 the initialisation of a program by the C library which requires a region 180 mapper. */ 181 182 long ExternalPager::attach(address_t *start, address_t size, map_flags_t flags, 183 l4_cap_idx_t ds, address_t offset, 184 unsigned char align) 185 { 186 long err = ExecPager::find(start, &size, flags, offset, align); 187 188 if (!err) 189 { 190 /* Attach the provided dataspace. 191 192 This is only done in this implementation to support the paging 193 mechanism. In a region mapper residing within the actual task, the 194 dataspace's map operation would be invoked to obtain mappings. */ 195 196 l4_addr_t ds_start; 197 err = ipc_attach_dataspace(ds, size, (void **) &ds_start); 198 199 if (err) 200 return err; 201 202 l4_touch_rw((const void *) ds_start, size); 203 204 MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds, ds_start); 205 add(r); 206 } 207 208 return err; 209 } 210 211 212 213 /* Receive signals from a task. */ 214 215 long ExternalPager::signal(unsigned long sig, unsigned long val) 216 { 217 notify_all(NOTIFY_TASK_SIGNAL, (notify_values_t) {sig, val}); 218 219 /* Handle the termination event. */ 220 221 if (sig == 0) 222 { 223 /* Once the program exits, the IPC gate connecting the program with its 224 internal pager can be released. */ 225 226 if (l4_is_valid_cap(_ipc_gate)) 227 { 228 l4_task_unmap(L4RE_THIS_TASK_CAP, l4_obj_fpage(_ipc_gate, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); 229 _ipc_gate = L4_INVALID_CAP; 230 } 231 232 /* Upon exit of the program's internal pager, release the capabilities 233 associated with this server. */ 234 235 else if (l4_is_valid_cap(_task)) 236 { 237 /* NOTE: Capability indexes to be obtained from the process creation 238 activity. */ 239 240 /* Parent and pager/region mapper. */ 241 242 l4_task_unmap(_task, l4_obj_fpage(0x13UL << L4_CAP_SHIFT, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); 243 l4_task_unmap(_task, l4_obj_fpage(0x19UL << L4_CAP_SHIFT, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); 244 } 245 } 246 247 return L4_EOK; 248 } 249 250 251 252 /* Subscribe to notifications. */ 253 254 long ExternalPager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) 255 { 256 return NotificationSupport::subscribe(notifier, flags); 257 } 258 259 /* Unsubscribe from notifications. */ 260 261 long ExternalPager::unsubscribe(l4_cap_idx_t notifier) 262 { 263 return NotificationSupport::unsubscribe(notifier); 264 } 265 266 /* vim: tabstop=2 expandtab shiftwidth=2 267 */