1 /* This file is part of the program psim.
3 Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include "device_table.h"
32 vm - virtual memory device for user simulation modes
36 In user mode, mapped text, data and stack addresses are managed by
37 the core. Unmapped addresses are passed onto this device (because
38 it establishes its self as the fallback device) for processing.
40 During initialization, children of this device will request the
41 mapping of the initial text and data segments. Those requests are
42 passed onto the core device so that that may establish the initial
45 Once the simulation has started (as noted above) any access to an
46 unmapped address range will be passed down to this device as an IO
47 access. This device will then either attach additional memory to
48 the core device or signal the access as being invalid.
50 The IOCTL function is used to notify this device of any changes to
51 the users `brk' point.
57 Specifies the lower address of the stack segment in the users
58 virtual address space. The initial stack page is defined by
59 stack-base + nr-bytes.
63 Specifies the maximum size of the stack segment in the users
68 typedef struct _hw_vm_device
{
69 /* area of memory valid for stack addresses */
70 unsigned_word stack_base
; /* min possible stack value */
71 unsigned_word stack_bound
;
72 unsigned_word stack_lower_limit
;
73 /* area of memory valid for heap addresses */
74 unsigned_word heap_base
;
75 unsigned_word heap_bound
;
76 unsigned_word heap_upper_limit
;
81 hw_vm_init_address_callback(device
*me
)
83 hw_vm_device
*vm
= (hw_vm_device
*)device_data(me
);
85 /* revert the stack/heap variables to their defaults */
86 vm
->stack_base
= device_find_integer_property(me
, "stack-base");
87 vm
->stack_bound
= (vm
->stack_base
88 + device_find_integer_property(me
, "nr-bytes"));
89 vm
->stack_lower_limit
= vm
->stack_bound
;
92 vm
->heap_upper_limit
= 0;
94 /* establish this device as the default memory handler */
95 device_attach_address(device_parent(me
),
98 0 /*address space - ignore*/,
100 (((unsigned)0)-1) /*nr_bytes - ignore*/,
101 access_read_write
/*access*/,
107 hw_vm_attach_address(device
*me
,
114 device
*who
) /*callback/default*/
116 hw_vm_device
*vm
= (hw_vm_device
*)device_data(me
);
117 /* update end of bss if necessary */
118 if (vm
->heap_base
< addr
+ nr_bytes
) {
119 vm
->heap_base
= addr
+ nr_bytes
;
120 vm
->heap_bound
= addr
+ nr_bytes
;
121 vm
->heap_upper_limit
= addr
+ nr_bytes
;
123 device_attach_address(device_parent(me
),
135 hw_vm_add_space(device
*me
,
141 hw_vm_device
*vm
= (hw_vm_device
*)device_data(me
);
142 unsigned_word block_addr
;
143 unsigned block_nr_bytes
;
145 /* an address in the stack area, allocate just down to the addressed
147 if (addr
>= vm
->stack_base
&& addr
< vm
->stack_lower_limit
) {
148 block_addr
= FLOOR_PAGE(addr
);
149 block_nr_bytes
= vm
->stack_lower_limit
- block_addr
;
150 vm
->stack_lower_limit
= block_addr
;
152 /* an address in the heap area, allocate all of the required heap */
153 else if (addr
>= vm
->heap_upper_limit
&& addr
< vm
->heap_bound
) {
154 block_addr
= vm
->heap_upper_limit
;
155 block_nr_bytes
= vm
->heap_bound
- vm
->heap_upper_limit
;
156 vm
->heap_upper_limit
= vm
->heap_bound
;
158 /* oops - an invalid address - abort the cpu */
159 else if (processor
!= NULL
) {
160 cpu_halt(processor
, cia
, was_signalled
, SIGSEGV
);
163 /* 2*oops - an invalid address and no processor */
168 /* got the parameters, allocate the space */
169 device_attach_address(device_parent(me
),
170 "vm@0x0,0", /* stop remap */
177 return block_nr_bytes
;
182 hw_vm_io_read_buffer_callback(device
*me
,
190 if (hw_vm_add_space(me
, addr
, nr_bytes
, processor
, cia
) >= nr_bytes
) {
191 memset(dest
, 0, nr_bytes
); /* always initialized to zero */
200 hw_vm_io_write_buffer_callback(device
*me
,
208 if (hw_vm_add_space(me
, addr
, nr_bytes
, processor
, cia
) >= nr_bytes
) {
209 return device_dma_write_buffer(device_parent(me
), source
,
212 0/*violate_read_only*/);
220 hw_vm_ioctl_callback(device
*me
,
225 /* While the caller is notified that the heap has grown by the
226 requested amount, the heap is actually extended out to a page
228 hw_vm_device
*vm
= (hw_vm_device
*)device_data(me
);
229 unsigned_word requested_break
= va_arg(ap
, unsigned_word
);
230 unsigned_word new_break
= ALIGN_8(requested_break
);
231 unsigned_word old_break
= vm
->heap_bound
;
232 signed_word delta
= new_break
- old_break
;
234 vm
->heap_bound
= ALIGN_PAGE(new_break
);
239 static device_callbacks
const hw_vm_callbacks
= {
240 { hw_vm_init_address_callback
, },
241 { hw_vm_attach_address
,
242 passthrough_device_address_detach
, },
243 { hw_vm_io_read_buffer_callback
,
244 hw_vm_io_write_buffer_callback
, },
245 { NULL
, passthrough_device_dma_write_buffer
, },
246 { NULL
, }, /* interrupt */
247 { generic_device_unit_decode
,
248 generic_device_unit_encode
, },
250 hw_vm_ioctl_callback
,
255 hw_vm_create(const char *name
,
256 const device_unit
*address
,
260 hw_vm_device
*vm
= ZALLOC(hw_vm_device
);
264 const device_descriptor hw_vm_device_descriptor
[] = {
265 { "vm", hw_vm_create
, &hw_vm_callbacks
},