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