paul@355 | 1 | /* |
paul@431 | 2 | * Common system pager functionality. |
paul@355 | 3 | * |
paul@504 | 4 | * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@355 | 5 | * |
paul@355 | 6 | * This program is free software; you can redistribute it and/or |
paul@355 | 7 | * modify it under the terms of the GNU General Public License as |
paul@355 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@355 | 9 | * the License, or (at your option) any later version. |
paul@355 | 10 | * |
paul@355 | 11 | * This program is distributed in the hope that it will be useful, |
paul@355 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@355 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@355 | 14 | * GNU General Public License for more details. |
paul@355 | 15 | * |
paul@355 | 16 | * You should have received a copy of the GNU General Public License |
paul@355 | 17 | * along with this program; if not, write to the Free Software |
paul@355 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@355 | 19 | * Boston, MA 02110-1301, USA |
paul@355 | 20 | */ |
paul@355 | 21 | |
paul@431 | 22 | #include <l4/re/env.h> |
paul@431 | 23 | #include <l4/re/c/rm.h> |
paul@431 | 24 | |
paul@431 | 25 | #include <mem/memory_utils.h> |
paul@431 | 26 | |
paul@355 | 27 | #include "pager.h" |
paul@355 | 28 | |
paul@355 | 29 | |
paul@355 | 30 | |
paul@431 | 31 | /* Initialise common functionality. */ |
paul@431 | 32 | |
paul@431 | 33 | ExecPager::ExecPager(address_t start, address_t end) |
paul@431 | 34 | : _start(start), _end(end) |
paul@431 | 35 | { |
paul@431 | 36 | } |
paul@431 | 37 | |
paul@366 | 38 | /* Necessary virtual destructor. */ |
paul@355 | 39 | |
paul@366 | 40 | ExecPager::~ExecPager() |
paul@355 | 41 | { |
paul@355 | 42 | } |
paul@355 | 43 | |
paul@504 | 44 | |
paul@504 | 45 | |
paul@431 | 46 | /* Add a region to the pager. */ |
paul@431 | 47 | |
paul@431 | 48 | void ExecPager::add(MappedRegion ®ion) |
paul@431 | 49 | { |
paul@431 | 50 | _regions[region.start] = region; |
paul@431 | 51 | } |
paul@431 | 52 | |
paul@504 | 53 | /* Remove a region from the pager. */ |
paul@504 | 54 | |
paul@504 | 55 | void ExecPager::remove(MappedRegion ®ion) |
paul@504 | 56 | { |
paul@504 | 57 | _regions.erase(region.start); |
paul@504 | 58 | } |
paul@504 | 59 | |
paul@504 | 60 | |
paul@504 | 61 | |
paul@431 | 62 | /* Attach a region for provision when page faults occur. */ |
paul@431 | 63 | |
paul@431 | 64 | long ExecPager::find(address_t *start, address_t *size, map_flags_t flags, |
paul@431 | 65 | address_t offset, unsigned char align) |
paul@431 | 66 | { |
paul@431 | 67 | if (align < L4_PAGESHIFT) |
paul@431 | 68 | align = L4_PAGESHIFT; |
paul@431 | 69 | |
paul@431 | 70 | /* Obtain the alignment increment and a properly aligned size. */ |
paul@431 | 71 | |
paul@431 | 72 | address_t increment = 1UL << align; |
paul@431 | 73 | address_t region_size = round(*size, increment); |
paul@431 | 74 | |
paul@431 | 75 | /* Either attempt to find an address for the specified region, starting from |
paul@431 | 76 | any indicated address. */ |
paul@431 | 77 | |
paul@431 | 78 | if (flags & L4RE_RM_F_SEARCH_ADDR) |
paul@431 | 79 | { |
paul@431 | 80 | /* Align any desired location. */ |
paul@431 | 81 | |
paul@431 | 82 | address_t region_start = trunc(*start, increment); |
paul@431 | 83 | |
paul@431 | 84 | /* Enforce a minimum address. */ |
paul@431 | 85 | |
paul@431 | 86 | if (region_start < _start) |
paul@431 | 87 | region_start = round(_start, increment); |
paul@431 | 88 | |
paul@431 | 89 | /* Search for existing regions after the desired, conformant address. */ |
paul@431 | 90 | |
paul@431 | 91 | MappedRegions::iterator it = _regions.upper_bound(region_start); |
paul@431 | 92 | |
paul@431 | 93 | /* Consider any region preceding or encompassing the desired address. */ |
paul@431 | 94 | |
paul@431 | 95 | if (it != _regions.begin()) |
paul@431 | 96 | { |
paul@431 | 97 | MappedRegions::iterator next = it; |
paul@431 | 98 | |
paul@431 | 99 | /* Step back to the preceding region to get its details. */ |
paul@431 | 100 | |
paul@431 | 101 | it--; |
paul@431 | 102 | MappedRegion &pr = it->second; |
paul@431 | 103 | address_t previous_end = pr.start + pr.size; |
paul@431 | 104 | it = next; |
paul@431 | 105 | |
paul@431 | 106 | /* The preceding region may displace the desired region location if it |
paul@431 | 107 | is encompassed by the region. */ |
paul@431 | 108 | |
paul@431 | 109 | if (region_start < previous_end) |
paul@431 | 110 | region_start = round(previous_end, increment); |
paul@431 | 111 | } |
paul@431 | 112 | |
paul@431 | 113 | /* Consider the regions following the current region start candidate. */ |
paul@431 | 114 | |
paul@431 | 115 | while ((it != _regions.end()) && !(_end && ((region_start + region_size) > _end))) |
paul@431 | 116 | { |
paul@431 | 117 | MappedRegion &r = it->second; |
paul@431 | 118 | |
paul@431 | 119 | /* Obtain the limit of available space being considered. */ |
paul@431 | 120 | |
paul@431 | 121 | address_t end_limit = r.start; |
paul@431 | 122 | |
paul@431 | 123 | /* Investigate subsequent regions if not enough space exists between the |
paul@431 | 124 | preceding region (or start of memory) and the current region. */ |
paul@431 | 125 | |
paul@431 | 126 | if ((region_start + region_size) > end_limit) |
paul@431 | 127 | { |
paul@431 | 128 | region_start = round(r.start + r.size, increment); |
paul@431 | 129 | it++; |
paul@431 | 130 | } |
paul@431 | 131 | |
paul@431 | 132 | /* Otherwise, the region can be positioned. */ |
paul@431 | 133 | |
paul@431 | 134 | else |
paul@431 | 135 | break; |
paul@431 | 136 | } |
paul@431 | 137 | |
paul@431 | 138 | /* Test for enough memory constrained by any predefined limit. */ |
paul@431 | 139 | |
paul@431 | 140 | if (_end && ((region_start + region_size) > _end)) |
paul@431 | 141 | return -L4_ENOMEM; |
paul@431 | 142 | |
paul@431 | 143 | /* Return the configured start and size. */ |
paul@431 | 144 | |
paul@431 | 145 | *start = region_start; |
paul@431 | 146 | *size = region_size; |
paul@431 | 147 | return L4_EOK; |
paul@431 | 148 | } |
paul@431 | 149 | |
paul@431 | 150 | /* Or attempt to add the specified region at a specific address. */ |
paul@431 | 151 | |
paul@431 | 152 | else |
paul@431 | 153 | { |
paul@431 | 154 | // NOTE: To be implemented. |
paul@431 | 155 | |
paul@431 | 156 | return -L4_ENOMEM; |
paul@431 | 157 | } |
paul@431 | 158 | } |
paul@431 | 159 | |
paul@355 | 160 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@355 | 161 | */ |