summaryrefslogtreecommitdiff
path: root/libhurd-mm/physmem-user.c
blob: 0ee13c9d36fdd1fc7dbdd44f0e93fc1bfae8e6ac (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
/* physmem-user.c - physmem client stubs.
   Copyright (C) 2004, 2005 Free Software Foundation, Inc.
   Written by Neal H. Walfield <neal@gnu.org>.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.
   
   The 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
   General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with the GNU Hurd; see the file COPYING.  If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
   USA.  */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdint.h>
#include <l4.h>

#include <compiler.h>
#include <hurd/startup.h>

#include "physmem-user.h"

/* Initialized by the machine-specific startup-code.  */
extern struct hurd_startup_data *__hurd_startup_data;

#define physmem (__hurd_startup_data->image.server)

/* Create a container managed by the physical memory server.  On
   success, returned in *CONTAINER.  */
error_t
hurd_pm_container_create (hurd_pm_container_t control,
			  hurd_pm_container_t *container)

{
  l4_msg_t msg;
  l4_msg_tag_t tag;

  l4_msg_clear (msg);
  l4_set_msg_label (msg, hurd_pm_container_create_id);
  l4_msg_append_word (msg, control);
  l4_msg_load (msg);

  tag = l4_call (physmem);
  l4_msg_store (tag, msg);

  *container = l4_msg_word (msg, 0);

  return l4_msg_label (msg);
}

/* Create a limited access capability for container CONTAINER, return
   in *ACCESS.  */
error_t
hurd_pm_container_share (hurd_pm_control_t container,
			 hurd_pm_container_t *access)
{
  l4_msg_t msg;
  l4_msg_tag_t tag;

  l4_msg_clear (msg);
  l4_set_msg_label (msg, hurd_pm_container_share_id);
  l4_msg_append_word (msg, container);
  l4_msg_load (msg);

  tag = l4_call (physmem);
  l4_msg_store (tag, msg);

  *access = l4_msg_word (msg, 0);

  return l4_msg_label (msg);
}

/* Allocate SIZE bytes according to FLAGS into container CONTAINER
   starting with index START.  *AMOUNT contains the number of bytes
   successfully allocated.  */
error_t
hurd_pm_container_allocate (hurd_pm_container_t container,
			    l4_word_t start, l4_word_t size,
			    l4_word_t flags, l4_word_t *amount)
{
  l4_msg_t msg;
  l4_msg_tag_t tag;

  l4_msg_clear (msg);
  l4_set_msg_label (msg, hurd_pm_container_allocate_id);
  l4_msg_append_word (msg, container);
  l4_msg_append_word (msg, flags);
  l4_msg_append_word (msg, start);
  l4_msg_append_word (msg, size);

  l4_msg_load (msg);

  tag = l4_call (physmem);
  l4_msg_store (tag, msg);

  *amount = l4_msg_word (msg, 0);

  return l4_msg_label (msg);
}

error_t
hurd_pm_container_deallocate (hurd_pm_container_t container,
			      uintptr_t start, uintptr_t size)
{
  l4_msg_t msg;
  l4_msg_tag_t tag;

  l4_msg_clear (msg);
  l4_set_msg_label (msg, hurd_pm_container_deallocate_id);
  l4_msg_append_word (msg, container);
  l4_msg_append_word (msg, start);
  l4_msg_append_word (msg, size);

  l4_msg_load (msg);

  tag = l4_call (physmem);
  l4_msg_store (tag, msg);

  return l4_msg_label (msg);
}
					     

/* Map the COUNT bytes of physical memory in container CONTAINER
   starting at byte INDEX at virtual memory address VADDR of the
   calling task according to the flags FLAGS.  */
error_t
hurd_pm_container_map (hurd_pm_container_t container,
		       l4_word_t index, size_t count,
		       uintptr_t vaddr, l4_word_t flags,
		       size_t *amountp)
{
  l4_msg_t msg;
  l4_msg_tag_t tag;
  int i;
  size_t amount;

  /* Let physmem take over the address space completely.  */
  l4_accept (l4_map_grant_items (L4_COMPLETE_ADDRESS_SPACE));

  l4_msg_clear (msg);
  l4_set_msg_label (msg, hurd_pm_container_map_id);
  l4_msg_append_word (msg, container);
  l4_msg_append_word (msg, flags);
  l4_msg_append_word (msg, vaddr);
  l4_msg_append_word (msg, index);
  l4_msg_append_word (msg, count);
  l4_msg_load (msg);

  tag = l4_call (physmem);
  l4_msg_store (tag, msg);

  if (amountp)
    {
      for (i = 0, amount = 0;
	   i < l4_typed_words (tag);
	   i += sizeof (l4_map_item_t) / sizeof (l4_word_t))
	{
	  l4_map_item_t mi;
	  l4_msg_get_map_item (msg, i, &mi);
	  assert (l4_is_map_item (mi));
	  amount += l4_size (l4_map_item_snd_fpage (mi));
	}

      *amountp = amount;
    }

  return l4_msg_label (msg);
}

error_t
hurd_pm_container_copy (hurd_pm_container_t src_container,
			uintptr_t src_start,
			hurd_pm_container_t dest_container,
			uintptr_t dest_start,
			size_t count,
			uintptr_t flags,
			size_t *amount)
{
  l4_msg_t msg;
  l4_msg_tag_t tag;

  l4_msg_clear (msg);
  l4_set_msg_label (msg, hurd_pm_container_copy_id);
  l4_msg_append_word (msg, src_container);
  l4_msg_append_word (msg, src_start);
  l4_msg_append_word (msg, dest_container);
  l4_msg_append_word (msg, dest_start);
  l4_msg_append_word (msg, count);
  l4_msg_append_word (msg, flags);

  l4_msg_load (msg);

  tag = l4_call (physmem);
  l4_msg_store (tag, msg);

  *amount = l4_msg_word (msg, 0);

  return l4_msg_label (msg);
}