summaryrefslogtreecommitdiff
path: root/libhurd-mm/mmap.c
blob: 425f6b420530705f98eb9b0ebd86e21f2357377b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* mmap.c - mmap implementation.
   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
   Written by Neal H. Walfield <neal@gnu.org>.

   This file is part of the GNU Hurd.

   GNU Hurd is free software: you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with GNU Hurd.  If not, see
   <http://www.gnu.org/licenses/>.  */


#include <hurd/stddef.h>
#include <viengoos/addr.h>
#include <hurd/as.h>
#include <hurd/storage.h>
#include <hurd/anonymous.h>
#include <hurd/map.h>

#include <sys/mman.h>
#include <stdint.h>

void *
mmap (void *addr, size_t length, int protect, int flags,
      int filedes, off_t offset)
{
  if (length == 0)
    return MAP_FAILED;

  if ((flags & ~(MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED)))
    panic ("mmap called with invalid flags");


  enum map_access access = 0;
  if ((PROT_READ & protect) || (PROT_EXEC & protect))
    access |= MAP_ACCESS_READ;
  if ((PROT_READ & protect))
    access |= MAP_ACCESS_WRITE;

  if ((protect & ~(PROT_READ | PROT_WRITE)))
    panic ("mmap called with unsupported protection: %x", protect);


  if ((flags & MAP_FIXED))
    /* Interpret ADDR exactly.  */
    {
      if (((uintptr_t) addr & (PAGESIZE - 1)))
	{
	  debug (0, "MAP_FIXED passed but address not page aligned: %p",
		 addr);
	  return MAP_FAILED;
	}

      /* Unmap any existing mapping.  */
      if (munmap (addr, length) == -1)
	return MAP_FAILED;
    }
  else
    {
      /* Round the address down to a multiple of the pagesize.  */
      addr = (void *) ((uintptr_t) addr & ~(PAGESIZE - 1));
    }

  /* Round length up.  */
  length = (length + PAGESIZE - 1) & ~(PAGESIZE - 1);

  if (addr)
    debug (5, "Trying to allocate memory %p-%p", addr, addr + length);

  struct anonymous_pager *pager;
  pager = anonymous_pager_alloc (VG_ADDR_VOID, addr, length, access,
				 VG_OBJECT_POLICY_DEFAULT,
				 (flags & MAP_FIXED) ? ANONYMOUS_FIXED: 0,
				 NULL, &addr);
  if (! pager)
    {
      debug (0, "Failed to create pager!");
      return MAP_FAILED;
    }

  debug (5, "Allocated memory %p-%p", addr, addr + length);

  return addr;
}

int
munmap (void *addr, size_t length)
{
  uintptr_t start = (uintptr_t) addr;
  uintptr_t end = start + length - 1;

  debug (5, "(%p, %x (%p))", addr, length, (void *) end);

  struct region region = { (uintptr_t) addr, length };

  /* There is a race. We can't hold MAPS_LOCK when we call
     PAGER->DESTROY.  In particular, the destroy function may also
     want to unmap some memory, which requires the maps_lock.  We
     can't grab one, drop the lock, destroy and repeat until all the
     pagers in the region are gone: another call to mmap could reuse a
     region we unmaped.  Then we'd unmap that new region.

     To make unmap atomic, we grab the lock, disconnect all maps in
     the region, add each to a list, drop the lock and then destroy
     each map on the list.  */
  struct map *list = NULL;

  maps_lock_lock ();

  /* Find any pager that overlaps within the designated region.  */
  struct map *map = map_find (region);
  if (! map)
    /* There are none.  We're done.  */
    {
      maps_lock_unlock ();
      return 0;
    }

  /* There may be pagers that come lexically before as well as after
     PAGER.  We start with PAGER and scan forward and then do the same
     but scan backwards.  As we disconnect PAGER, we need to remember
     its previous pointer.  */
  struct map *prev = hurd_btree_map_prev (map);

  int dir;
  for (dir = 0; dir < 2; dir ++, map = prev)
    while (map)
      {
	struct map *next = (dir == 0 ? hurd_btree_map_next (map)
			    : hurd_btree_map_prev (map));

	uintptr_t map_start = map->region.start;
	uintptr_t map_end = map_start + map->region.length - 1;

	debug (5, "(%x-%x): considering %x-%x",
	       start, end, map_start, map_end);

	if (map_start > end || map_end < start)
	  break;

	if (map_start < start)
	  /* We only want to unmap the latter part of the region.
	     Split it and set map to that part.  */
	  {
	    debug (5, "(%x-%x): splitting %x-%x at offset %x",
		   start, end, map_start, map_end, start - map_start);

	    struct map *second = map_split (map, start - map_start);
	    if (second)
	      map = second;
	    else
	      {
		panic ("munmap (%x-%x) but cannot split map at %x-%x",
		       start, end, map_start, map_end);
	      }

	    map_start = start;
	  }

	if (map_end > end)
	  /* We only want to unmap the former part of the region.
	     Split it and set map to that part.  */
	  {
	    debug (5, "(%x-%x): splitting %x-%x at offset %x",
		   start, end, map_start, map_end, end - map_start + 1);

	    struct map *second = map_split (map, end - map_start + 1);
	    if (! second)
	      {
		panic ("munmap (%x-%x) but cannot split map at %x-%x",
		       start, end, map_start, map_end);
	      }

	    map_end = end;
	  }

	/* Unlink and add to the list.  */
	debug (5, "(%x-%x): removing %x-%x",
	       start, end, map_start, map_end);

	map_disconnect (map);
	map->next = list;
	list = map;

	map = next;
      }

  maps_lock_unlock ();

  /* Destroy the maps.  */
  map = list;
  while (map)
    {
      struct map *next = map->next;

      uintptr_t map_start = map->region.start;
      uintptr_t map_end = map_start + map->region.length - 1;
      debug (5, "Detroying pager covering %x-%x (%d pages)",
	     map_start, map_end,
	     (int) (map_end - map_start + 1) / PAGESIZE);

      map_destroy (map);

      map = next;
    }

  return 0;
}