paul@65 | 1 | #include "pipe_pager.h" |
paul@65 | 2 | #include "pipe_object_server.h" |
paul@65 | 3 | |
paul@65 | 4 | /* Initialise a pager for a pipe with a shared page mapper for moderating |
paul@65 | 5 | access to loaded pages. At first, no mapper will be configured: this must be |
paul@65 | 6 | done by requesting a region. */ |
paul@65 | 7 | |
paul@66 | 8 | PipePager::PipePager(PipePaging *paging, bool writing) |
paul@66 | 9 | : Pager(NULL, writing ? L4_FPAGE_RW : L4_FPAGE_RO), |
paul@66 | 10 | _paging(paging), _writing(writing) |
paul@65 | 11 | { |
paul@66 | 12 | /* Initialise the size of the paged region. */ |
paul@65 | 13 | |
paul@66 | 14 | _size = _paging->region_size(); |
paul@65 | 15 | } |
paul@65 | 16 | |
paul@65 | 17 | int PipePager::expected_items() |
paul@65 | 18 | { |
paul@65 | 19 | return PipeObject_expected_items; |
paul@65 | 20 | } |
paul@65 | 21 | |
paul@65 | 22 | ipc_server_handler_type PipePager::handler() |
paul@65 | 23 | { |
paul@65 | 24 | return (ipc_server_handler_type) handle_PipeObject; |
paul@65 | 25 | } |
paul@65 | 26 | |
paul@65 | 27 | |
paul@65 | 28 | |
paul@67 | 29 | /* Close the pager, releasing the paging coordinator for the pipe. This will |
paul@67 | 30 | release all active page mappers. */ |
paul@67 | 31 | |
paul@67 | 32 | void PipePager::close() |
paul@67 | 33 | { |
paul@67 | 34 | _paging->detach(); |
paul@67 | 35 | } |
paul@67 | 36 | |
paul@65 | 37 | /* Support paging. */ |
paul@65 | 38 | |
paul@65 | 39 | long PipePager::map(unsigned long offset, l4_addr_t hot_spot, flags_t flags, l4_snd_fpage_t *region) |
paul@65 | 40 | { |
paul@65 | 41 | return Pager::map(offset, hot_spot, flags, region); |
paul@65 | 42 | } |
paul@65 | 43 | |
paul@65 | 44 | |
paul@65 | 45 | |
paul@65 | 46 | /* Return details of the current region. */ |
paul@65 | 47 | |
paul@65 | 48 | long PipePager::current_region(offset_t *populated_size, offset_t *size) |
paul@65 | 49 | { |
paul@65 | 50 | if (_mapper != NULL) |
paul@65 | 51 | { |
paul@65 | 52 | *populated_size = _mapper->get_data_size(); |
paul@65 | 53 | *size = _size; |
paul@65 | 54 | return L4_EOK; |
paul@65 | 55 | } |
paul@65 | 56 | else |
paul@65 | 57 | return -L4_EIO; |
paul@65 | 58 | } |
paul@65 | 59 | |
paul@65 | 60 | /* Obtain the next region and its details. */ |
paul@65 | 61 | |
paul@65 | 62 | long PipePager::next_region(offset_t *populated_size, offset_t *size) |
paul@65 | 63 | { |
paul@65 | 64 | /* Obtain a new region if writing. */ |
paul@65 | 65 | |
paul@66 | 66 | if (_writing) |
paul@65 | 67 | return next_region_for_writer(populated_size, size); |
paul@65 | 68 | else |
paul@65 | 69 | return next_region_for_reader(populated_size, size); |
paul@65 | 70 | } |
paul@65 | 71 | |
paul@65 | 72 | long PipePager::next_region_for_reader(offset_t *populated_size, offset_t *size) |
paul@65 | 73 | { |
paul@65 | 74 | /* Obtain the next region if a current region is defined. */ |
paul@65 | 75 | |
paul@65 | 76 | if (_mapper != NULL) |
paul@65 | 77 | { |
paul@65 | 78 | PageMapper *mapper = _paging->next_region(); |
paul@65 | 79 | |
paul@65 | 80 | /* Return an error if no next region currently exists. */ |
paul@65 | 81 | |
paul@65 | 82 | if (mapper == NULL) |
paul@65 | 83 | return -L4_EIO; |
paul@65 | 84 | |
paul@65 | 85 | _mapper = mapper; |
paul@65 | 86 | } |
paul@65 | 87 | |
paul@65 | 88 | /* Obtain the first region if no mapper is defined. */ |
paul@65 | 89 | |
paul@65 | 90 | else |
paul@65 | 91 | _mapper = _paging->first_region(); |
paul@65 | 92 | |
paul@65 | 93 | /* Return the details of the now-current region. */ |
paul@65 | 94 | |
paul@65 | 95 | return current_region(populated_size, size); |
paul@65 | 96 | } |
paul@65 | 97 | |
paul@65 | 98 | long PipePager::next_region_for_writer(offset_t *populated_size, offset_t *size) |
paul@65 | 99 | { |
paul@65 | 100 | /* Set the populated size before moving on. */ |
paul@65 | 101 | |
paul@65 | 102 | if (_mapper != NULL) |
paul@65 | 103 | _mapper->set_data_size(*populated_size); |
paul@65 | 104 | |
paul@65 | 105 | /* A guaranteed amount of memory is reserved for the pipe. */ |
paul@65 | 106 | |
paul@66 | 107 | PagesConserving *pages = _paging->pages(); |
paul@66 | 108 | |
paul@66 | 109 | if (!pages->increase(_size)) |
paul@65 | 110 | return -L4_ENOMEM; |
paul@65 | 111 | |
paul@65 | 112 | /* Set the page mapper for the region. */ |
paul@65 | 113 | |
paul@66 | 114 | _mapper = new PageMapper(_paging->accessor(), pages); |
paul@65 | 115 | _mapper->attach(); |
paul@65 | 116 | |
paul@65 | 117 | /* Record the mapper as the latest region in the accessor. */ |
paul@65 | 118 | |
paul@65 | 119 | _paging->add_region(_mapper); |
paul@65 | 120 | |
paul@65 | 121 | /* Set the region size to the allocated amount. */ |
paul@65 | 122 | |
paul@65 | 123 | *size = _size; |
paul@65 | 124 | *populated_size = 0; |
paul@65 | 125 | |
paul@65 | 126 | return L4_EOK; |
paul@65 | 127 | } |
paul@65 | 128 | |
paul@65 | 129 | // vim: tabstop=4 expandtab shiftwidth=4 |