summaryrefslogtreecommitdiff
path: root/libhurd-mm/madvise.c
blob: 853226d53c0dc8d56a6c7f36faf79792844a0634 (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
/* madvise.c - madvise implementation.
   Copyright (C) 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>

int
madvise (void *addr, size_t length, int advice)
{
  debug (5, "(%p, %x, %d)", addr, length, advice);

  if (((uintptr_t) addr & (PAGESIZE - 1)) != 0)
    {
      debug (0, "Address %p not multiple of pagesize.", addr);
      return EINVAL;
    }
  if ((length & (PAGESIZE - 1)) != 0)
    {
      debug (0, "Length %x not multiple of pagesize.", length);
      return EINVAL;
    }

  switch (advice)
    {
    case MADV_NORMAL:
      advice = pager_advice_normal;
      break;
    case MADV_RANDOM:
      advice = pager_advice_random;
      break;
    case MADV_SEQUENTIAL:
      advice = pager_advice_sequential;
      break;
    case MADV_WILLNEED:
      advice = pager_advice_willneed;
      break;
    case MADV_DONTNEED:
      advice = pager_advice_dontneed;
      break;
    default:
      return EINVAL;
    }

  uintptr_t start = (uintptr_t) addr;
  uintptr_t end = start + length - 1;

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

  debug (5, "Region: %x-%x",
	 region.start, region.start + region.length - 1);

  maps_lock_lock ();

  /* Find the first map that overlaps with the designated region.  */
  struct map *map;
  while (region.length > 0
	 && (map = hurd_btree_map_find_first (&maps, &region)))
    {
      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);

      /*
	map_start/map->offset                map_end
	/   \                           /  \
	v     v                         v    v
	+------------------------------------+
	|                                    | <- pager
	+------------------------------------+

	^     ^     ^                        ^     ^     ^
	\    |    /                          \    |   /
	start                                 end

      */
      uintptr_t s = map->offset;
      uintptr_t l = map->region.length;
      if (start > map_start)
	{
	  s += start - map_start;
	  l -= start - map_start;
	}
      if (end < map_end)
	l -= map_end - end;

      /* Advance region to just after MAP.  */
      if (map_end >= region.start + region.length)
	region.length = 0;
      else
	{
	  region.length -= map_end - region.start + 1;
	  region.start += map_end - region.start + 1;
	}

      struct pager *pager = map->pager;
      if (pager->advise)
	{
	  maps_lock_unlock ();
	  pager->advise (map->pager, s, l, advice);

	  /* If REGION.LENGTH is zero, don't bother to retake the
	     lock: we are done.  */
	  if (region.length > 0)
	    maps_lock_lock ();
	  else
	    return 0;
	}

      debug (5, "Region: %x-%x",
	     region.start, region.start + region.length - 1);
    }

  maps_lock_unlock ();

  return 0;
}

int
posix_madvise (void *addr, size_t len, int advice)
{
  return madvise (addr, len, advice);
}