aboutsummaryrefslogtreecommitdiff
path: root/sim/ppc/hw_vm.c
blob: 10987e09c5a5cd7303091c0fb3d5f924f0626176 (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*  This file is part of the program psim.
    
    Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
    
    This program 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 of the License, or
    (at your option) any later version.
    
    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    
    */


#ifndef _HW_VM_C_
#define _HW_VM_C_

#include "device_table.h"
#include "cpu.h"

#include <signal.h>

/* DEVICE

   vm - virtual memory device for user simulation modes

   DESCRIPTION

   In user mode, mapped text, data and stack addresses are managed by
   the core.  Unmapped addresses are passed onto this device (because
   it establishes its self as the fallback device) for processing.

   During initialization, children of this device will request the
   mapping of the initial text and data segments.  Those requests are
   passed onto the core device so that that may establish the initial
   memory regions.

   Once the simulation has started (as noted above) any access to an
   unmapped address range will be passed down to this device as an IO
   access.  This device will then either attach additional memory to
   the core device or signal the access as being invalid.

   The IOCTL function is used to notify this device of any changes to
   the users `brk' point.

   PROPERTIES

   stack-base = <number>

   Specifies the lower address of the stack segment in the users
   virtual address space.  The initial stack page is defined by
   stack-base + nr-bytes.

   nr-bytes = <number>

   Specifies the maximum size of the stack segment in the users
   address space.

   */

typedef struct _hw_vm_device {
  /* area of memory valid for stack addresses */
  unsigned_word stack_base; /* min possible stack value */
  unsigned_word stack_bound;
  unsigned_word stack_lower_limit;
  /* area of memory valid for heap addresses */
  unsigned_word heap_base;
  unsigned_word heap_bound;
  unsigned_word heap_upper_limit;
} hw_vm_device;


static void
hw_vm_init_address_callback(device *me)
{
  hw_vm_device *vm = (hw_vm_device*)device_data(me);

  /* revert the stack/heap variables to their defaults */
  vm->stack_base = device_find_integer_property(me, "stack-base");
  vm->stack_bound = (vm->stack_base
		     + device_find_integer_property(me, "nr-bytes"));
  vm->stack_lower_limit = vm->stack_bound;
  vm->heap_base = 0;
  vm->heap_bound = 0;
  vm->heap_upper_limit = 0;

  /* establish this device as the default memory handler */
  device_attach_address(device_parent(me),
			attach_callback + 1,
			0 /*address space - ignore*/,
			0 /*addr - ignore*/,
			(((unsigned)0)-1) /*nr_bytes - ignore*/,
			access_read_write /*access*/,
			me);
}


static void
hw_vm_attach_address(device *me,
		     attach_type attach,
		     int space,
		     unsigned_word addr,
		     unsigned nr_bytes,
		     access_type access,
		     device *client) /*callback/default*/
{
  hw_vm_device *vm = (hw_vm_device*)device_data(me);
  /* update end of bss if necessary */
  if (vm->heap_base < addr + nr_bytes) {
    vm->heap_base = addr + nr_bytes;
    vm->heap_bound = addr + nr_bytes;
    vm->heap_upper_limit = addr + nr_bytes;
  }
  device_attach_address(device_parent(me),
			attach_raw_memory,
			0 /*address space*/,
			addr,
			nr_bytes,
			access,
			me);
}


static unsigned
hw_vm_add_space(device *me,
		unsigned_word addr,
		unsigned nr_bytes,
		cpu *processor,
		unsigned_word cia)
{
  hw_vm_device *vm = (hw_vm_device*)device_data(me);
  unsigned_word block_addr;
  unsigned block_nr_bytes;

  /* an address in the stack area, allocate just down to the addressed
     page */
  if (addr >= vm->stack_base && addr < vm->stack_lower_limit) {
    block_addr = FLOOR_PAGE(addr);
    block_nr_bytes = vm->stack_lower_limit - block_addr;
    vm->stack_lower_limit = block_addr;
  }
  /* an address in the heap area, allocate all of the required heap */
  else if (addr >= vm->heap_upper_limit && addr < vm->heap_bound) {
    block_addr = vm->heap_upper_limit;
    block_nr_bytes = vm->heap_bound - vm->heap_upper_limit;
    vm->heap_upper_limit = vm->heap_bound;
  }
  /* oops - an invalid address - abort the cpu */
  else if (processor != NULL) {
    cpu_halt(processor, cia, was_signalled, SIGSEGV);
    return 0;
  }
  /* 2*oops - an invalid address and no processor */
  else {
    return 0;
  }

  /* got the parameters, allocate the space */
  device_attach_address(device_parent(me),
			attach_raw_memory,
			0 /*address space*/,
			block_addr,
			block_nr_bytes,
			access_read_write,
			me);
  return block_nr_bytes;
}


static unsigned
hw_vm_io_read_buffer_callback(device *me,
			   void *dest,
			   int space,
			   unsigned_word addr,
			   unsigned nr_bytes,
			   cpu *processor,
			   unsigned_word cia)
{
  if (hw_vm_add_space(me, addr, nr_bytes, processor, cia) >= nr_bytes) {
    memset(dest, 0, nr_bytes); /* always initialized to zero */
    return nr_bytes;
  }
  else 
    return 0;
}


static unsigned
hw_vm_io_write_buffer_callback(device *me,
			    const void *source,
			    int space,
			    unsigned_word addr,
			    unsigned nr_bytes,
			    cpu *processor,
			    unsigned_word cia)
{
  if (hw_vm_add_space(me, addr, nr_bytes, processor, cia) >= nr_bytes) {
    return device_dma_write_buffer(device_parent(me), source,
				   space, addr,
				   nr_bytes,
				   0/*violate_read_only*/);
  }
  else
    return 0;
}


static int
hw_vm_ioctl(device *me,
	    cpu *processor,
	    unsigned_word cia,
	    device_ioctl_request request,
	    va_list ap)
{
  /* While the caller is notified that the heap has grown by the
     requested amount, the heap is actually extended out to a page
     boundary. */
  hw_vm_device *vm = (hw_vm_device*)device_data(me);
  switch (request) {
  case device_ioctl_break:
    {
      unsigned_word requested_break = va_arg(ap, unsigned_word);
      unsigned_word new_break = ALIGN_8(requested_break);
      unsigned_word old_break = vm->heap_bound;
      signed_word delta = new_break - old_break;
      if (delta > 0)
	vm->heap_bound = ALIGN_PAGE(new_break);
      break;
    }
  default:
    device_error(me, "Unsupported ioctl request");
    break;
  }
  return 0;
    
}


static device_callbacks const hw_vm_callbacks = {
  { hw_vm_init_address_callback, },
  { hw_vm_attach_address,
    passthrough_device_address_detach, },
  { hw_vm_io_read_buffer_callback,
    hw_vm_io_write_buffer_callback, },
  { NULL, passthrough_device_dma_write_buffer, },
  { NULL, }, /* interrupt */
  { generic_device_unit_decode,
    generic_device_unit_encode, },
  NULL, /* instance */
  hw_vm_ioctl,
};


static void *
hw_vm_create(const char *name,
	     const device_unit *address,
	     const char *args)
{
  hw_vm_device *vm = ZALLOC(hw_vm_device);
  return vm;
}

const device_descriptor hw_vm_device_descriptor[] = {
  { "vm", hw_vm_create, &hw_vm_callbacks },
  { NULL },
};

#endif /* _HW_VM_C_ */