summaryrefslogtreecommitdiff
path: root/libhurd-mm/mm-init.c
blob: 896b69f91e3e0d5a1f63a1ddf5702bf979ae4d6a (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* mm-init.h - Memory management initialization.
   Copyright (C) 2004, 2005, 2008 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 <hurd/startup.h>
#include <hurd/exceptions.h>
#include <hurd/thread.h>

#ifdef i386
#include <hurd/pager.h>
#endif

#include <backtrace.h>

#include "storage.h"
#include "as.h"

extern struct hurd_startup_data *__hurd_startup_data;

vg_addr_t meta_data_activity;

int mm_init_done;

void
mm_init (vg_addr_t activity)
{
  assert (! mm_init_done);

  extern int output_debug;
  output_debug = 1;

  if (VG_ADDR_IS_VOID (activity))
    meta_data_activity = __hurd_startup_data->activity;
  else
    meta_data_activity = activity;

  hurd_activation_handler_init_early ();
  storage_init ();
  as_init ();
  hurd_activation_handler_init ();

  mm_init_done = 1;

#ifndef NDEBUG
  /* The following test checks the activation trampoline.  In
     particular, it checks that the register file before a fault
     matches the register file after a fault.  This is interesting
     because such an activation is handled in normal mode.  That
     means, when the fault occurs, we enter the activation handler,
     return an activation frame, enter normal mode, execute the normal
     mode activation handler, call the call back functions, and then
     return to the interrupted code.  */
#ifdef i386
  void test (int nesting)
  {
    vg_addr_t addr = as_alloc (PAGESIZE_LOG2, 1, true);
    void *a = VG_ADDR_TO_PTR (vg_addr_extend (addr, 0, PAGESIZE_LOG2));

    int recursed = false;

    struct storage storage;
    bool fault (struct pager *pager,
		uintptr_t offset, int count, bool ro,
		uintptr_t fault_addr, uintptr_t ip,
		struct vg_activation_fault_info info)
    {
      assert (a == (void *) (fault_addr & ~(PAGESIZE - 1)));
      assert (count == 1);

      struct hurd_utcb *utcb = hurd_utcb ();
      struct activation_frame *activation_frame = utcb->activation_stack;
      debug (4, "Fault at %p (ip: %p, sp: %p, eax: %p, "
	     "ebx: %p, ecx: %p, edx: %p, edi: %p, esi: %p, ebp: %p, "
	     "eflags: %p)",
	     fault,
	     (void *) activation_frame->eip,
	     (void *) activation_frame->esp,
	     (void *) activation_frame->eax,
	     (void *) activation_frame->ebx,
	     (void *) activation_frame->ecx,
	     (void *) activation_frame->edx,
	     (void *) activation_frame->edi,
	     (void *) activation_frame->esi,
	     (void *) activation_frame->ebp,
	     (void *) activation_frame->eflags);

      assert (activation_frame->eax == 0xa);
      assert (activation_frame->ebx == 0xb);
      assert (activation_frame->ecx == 0xc);
      assert (activation_frame->edx == 0xd);
      assert (activation_frame->edi == 0xd1);
      assert (activation_frame->esi == 0x21);
      assert (activation_frame->ebp == (uintptr_t) a);
      /* We cannot easily check esp and eip here.  */

      as_ensure (addr);
      storage = storage_alloc (VG_ADDR_VOID,
			       vg_cap_page, STORAGE_UNKNOWN,
			       VG_OBJECT_POLICY_DEFAULT,
			       addr);

      if (nesting > 1 && ! recursed)
	{
	  recursed = true;

	  int i;
	  for (i = 0; i < 3; i ++)
	    {
	      debug (5, "Depth: %d; iter: %d", nesting - 1, i);
	      test (nesting - 1);
	      debug (5, "Depth: %d; iter: %d done", nesting - 1, i);
	    }
	}

      return true;
    }

    struct pager pager = PAGER_VOID;
    pager.length = PAGESIZE;
    pager.fault = fault;
    pager_init (&pager);

    struct region region = { (uintptr_t) a, PAGESIZE };
    struct map *map = map_create (region, MAP_ACCESS_ALL, &pager, 0, NULL);

    uintptr_t pre_flags, pre_esp;
    uintptr_t eax, ebx, ecx, edx, edi, esi, ebp, esp, flags;
    uintptr_t canary;

    /* Check that the trampoline works.  */
    __asm__ __volatile__
      (
       "mov %%esp, %[pre_esp]\n\t"
       "pushf\n\t"
       "pop %%eax\n\t"
       "mov %%eax, %[pre_flags]\n\t"

       /* Canary.  */
       "pushl $0xcab00d1e\n\t"

       "pushl %%ebp\n\t"

       "mov $0xa, %%eax\n\t"
       "mov $0xb, %%ebx\n\t"
       "mov $0xc, %%ecx\n\t"
       "mov $0xd, %%edx\n\t"
       "mov $0xd1, %%edi\n\t"
       "mov $0x21, %%esi\n\t"
       "mov %[addr], %%ebp\n\t"
       /* Fault!  */
       "mov %%eax, 0(%%ebp)\n\t"

       /* Save the current ebp.  */
       "pushl %%ebp\n\t"
       /* Restore the old ebp.  */
       "mov 4(%%esp), %%ebp\n\t"

       /* Save the rest of the GP registers.  */
       "mov %%eax, %[eax]\n\t"
       "mov %%ebx, %[ebx]\n\t"
       "mov %%ecx, %[ecx]\n\t"
       "mov %%edx, %[edx]\n\t"
       "mov %%edi, %[edi]\n\t"
       "mov %%esi, %[esi]\n\t"

       /* Save the new flags.  */
       "pushf\n\t"
       "pop %%eax\n\t"
       "mov %%eax, %[flags]\n\t"

       /* Save the new ebp.  */
       "mov 0(%%esp), %%eax\n\t"
       "mov %%eax, %[ebp]\n\t"

       /* Fix up the stack.  */
       "add $8, %%esp\n\t"

       /* Grab the canary.  */
       "popl %%eax\n\t"
       "mov %%eax, %[canary]\n\t"

       /* And don't forget to save the new esp.  */
       "mov %%esp, %[esp]\n\t"

       : [eax] "=m" (eax), [ebx] "=m" (ebx),
	 [ecx] "=m" (ecx), [edx] "=m" (edx),
	 [edi] "=m" (edi), [esi] "=m" (esi), [ebp] "=m" (ebp),
	 [pre_esp] "=m" (pre_esp), [esp] "=m" (esp),
	 [pre_flags] "=m" (pre_flags), [flags] "=m" (flags),
	 [canary] "=m" (canary)
       : [addr] "m" (a)
       : "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi");

    debug (4, "Regsiter file: "
	   "eax: %p, ebx: %p, ecx: %p, edx: %p, "
	   "edi: %p, esi: %p, ebp: %p -> %p, esp: %p -> %p, flags: %p -> %p",
	   (void *) eax, (void *)  ebx, (void *)  ecx, (void *)  edx,
	   (void *) edi, (void *)  esi, (void *) a, (void *)  ebp,
	   (void *) pre_esp, (void *) esp,
	   (void *) pre_flags, (void *) flags);

    assert (eax == 0xa);
    assert (ebx == 0xb);
    assert (ecx == 0xc);
    assert (edx == 0xd);
    assert (edi == 0xd1);
    assert (esi == 0x21);
    assert (ebp == (uintptr_t) a);
    assert (esp == pre_esp);
    assert (flags == pre_flags);
    assert (canary == 0xcab00d1e);

    maps_lock_lock ();
    map_disconnect (map);
    maps_lock_unlock ();
    map_destroy (map);

    storage_free (storage.addr, false);
    as_free (addr, 1);
  }

  int i;
  for (i = 0; i < 3; i ++)
    {
      debug (5, "Depth: %d; iter: %d", 3, i + 1);
      test (3);
      debug (5, "Depth: %d; iter: %d done", 3, i + 1);
    }
#endif
#endif
}