1.1 --- a/libexec/lib/src/segment.cc Thu May 12 22:13:22 2022 +0200
1.2 +++ b/libexec/lib/src/segment.cc Sat May 14 23:52:24 2022 +0200
1.3 @@ -41,7 +41,10 @@
1.4 {
1.5 _region_base = trunc(_base, L4_PAGESIZE);
1.6 _region_offset = _base - _region_base;
1.7 - _region_size = round(_size, L4_PAGESIZE);
1.8 +
1.9 + /* Expand the region size. */
1.10 +
1.11 + _region_size = round(_size + _region_offset, L4_PAGESIZE);
1.12 }
1.13
1.14 /* Return the address of allocated memory. */
1.15 @@ -58,18 +61,11 @@
1.16 return _size;
1.17 }
1.18
1.19 -/* Allocate a region for the segment. */
1.20 +/* Allocate a writable region for the segment. */
1.21
1.22 long Segment::allocate()
1.23 {
1.24 - /* Make regions writable if they need to be filled. */
1.25 -
1.26 - l4re_rm_flags_t allocation_flags = _flags;
1.27 -
1.28 - if (_file_contents)
1.29 - allocation_flags |= L4RE_RM_F_W;
1.30 -
1.31 - return ipc_allocate_align(_size, L4RE_RM_F_SEARCH_ADDR | allocation_flags,
1.32 + return ipc_allocate_align(_region_size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW,
1.33 L4_PAGESHIFT, (void **) &_buf, &_ds);
1.34 }
1.35
1.36 @@ -77,10 +73,21 @@
1.37
1.38 long Segment::fill(file_t *file)
1.39 {
1.40 - if (!_file_contents)
1.41 - return L4_EOK;
1.42 + /* Allocate writable memory to populate. */
1.43 +
1.44 + long err = ipc_allocate_align(_region_size,
1.45 + L4RE_RM_F_SEARCH_ADDR | _flags | L4RE_RM_F_W,
1.46 + L4_PAGESHIFT, (void **) &_buf, &_ds);
1.47 +
1.48 + if (err)
1.49 + return err;
1.50 +
1.51 + /* Align the exposed file contents to the memory mapped region.
1.52 + Since the region is page-aligned, the region offset is introduced to map
1.53 + from an earlier part of the file, if appropriate. */
1.54
1.55 memset(_buf, 0, _region_size);
1.56 +
1.57 client_seek(file, _file_offset, SEEK_SET);
1.58 offset_t nread = client_read(file, _buf + _region_offset, _file_contents);
1.59
1.60 @@ -94,7 +101,7 @@
1.61
1.62 MappedRegion &Segment::region()
1.63 {
1.64 - _region = MappedRegion((l4_addr_t) _buf, page_order(_region_size), _flags, _region_base);
1.65 + _region = MappedRegion((l4_addr_t) _buf, _region_size, _flags, _region_base);
1.66 return _region;
1.67 }
1.68
1.69 @@ -110,5 +117,19 @@
1.70 return (address - (l4_addr_t) _buf) + _region_base;
1.71 }
1.72
1.73 +/* Return the amount of file content loaded into the segment. */
1.74 +
1.75 +offset_t Segment::file_contents()
1.76 +{
1.77 + return _file_contents;
1.78 +}
1.79 +
1.80 +/* Return the region flags for the segment. */
1.81 +
1.82 +l4re_rm_flags_t Segment::region_flags()
1.83 +{
1.84 + return _flags;
1.85 +}
1.86 +
1.87 /* vim: tabstop=2 expandtab shiftwidth=2
1.88 */