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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "device_table.h"
31 vm - virtual memory device for user simulation modes
35 In user mode, mapped text, data and stack addresses are managed by
36 the core. Unmapped addresses are passed onto this device (because
37 it establishes its self as the fallback device) for processing.
39 During initialization, children of this device will request the
40 mapping of the initial text and data segments. Those requests are
41 passed onto the core device so that that may establish the initial
44 Once the simulation has started (as noted above) any access to an
45 unmapped address range will be passed down to this device as an IO
46 access. This device will then either attach additional memory to
47 the core device or signal the access as being invalid.
49 The IOCTL function is used to notify this device of any changes to
50 the users `brk' point.
56 Specifies the lower address of the stack segment in the users
57 virtual address space. The initial stack page is defined by
58 stack-base + nr-bytes.
62 Specifies the maximum size of the stack segment in the users
67 typedef struct _hw_vm_device
{
68 /* area of memory valid for stack addresses */
69 unsigned_word stack_base
; /* min possible stack value */
70 unsigned_word stack_bound
;
71 unsigned_word stack_lower_limit
;
72 /* area of memory valid for heap addresses */
73 unsigned_word heap_base
;
74 unsigned_word heap_bound
;
75 unsigned_word heap_upper_limit
;
80 hw_vm_init_address_callback(device
*me
)
82 hw_vm_device
*vm
= (hw_vm_device
*)device_data(me
);
84 /* revert the stack/heap variables to their defaults */
85 vm
->stack_base
= device_find_integer_property(me
, "stack-base");
86 vm
->stack_bound
= (vm
->stack_base
87 + device_find_integer_property(me
, "nr-bytes"));
88 vm
->stack_lower_limit
= vm
->stack_bound
;
91 vm
->heap_upper_limit
= 0;
93 /* establish this device as the default memory handler */
94 device_attach_address(device_parent(me
),
96 0 /*address space - ignore*/,
98 (((unsigned)0)-1) /*nr_bytes - ignore*/,
99 access_read_write
/*access*/,
105 hw_vm_attach_address(device
*me
,
111 device
*client
) /*callback/default*/
113 hw_vm_device
*vm
= (hw_vm_device
*)device_data(me
);
114 /* update end of bss if necessary */
115 if (vm
->heap_base
< addr
+ nr_bytes
) {
116 vm
->heap_base
= addr
+ nr_bytes
;
117 vm
->heap_bound
= addr
+ nr_bytes
;
118 vm
->heap_upper_limit
= addr
+ nr_bytes
;
120 device_attach_address(device_parent(me
),
131 hw_vm_add_space(device
*me
,
137 hw_vm_device
*vm
= (hw_vm_device
*)device_data(me
);
138 unsigned_word block_addr
;
139 unsigned block_nr_bytes
;
141 /* an address in the stack area, allocate just down to the addressed
143 if (addr
>= vm
->stack_base
&& addr
< vm
->stack_lower_limit
) {
144 block_addr
= FLOOR_PAGE(addr
);
145 block_nr_bytes
= vm
->stack_lower_limit
- block_addr
;
146 vm
->stack_lower_limit
= block_addr
;
148 /* an address in the heap area, allocate all of the required heap */
149 else if (addr
>= vm
->heap_upper_limit
&& addr
< vm
->heap_bound
) {
150 block_addr
= vm
->heap_upper_limit
;
151 block_nr_bytes
= vm
->heap_bound
- vm
->heap_upper_limit
;
152 vm
->heap_upper_limit
= vm
->heap_bound
;
154 /* oops - an invalid address - abort the cpu */
155 else if (processor
!= NULL
) {
156 cpu_halt(processor
, cia
, was_signalled
, SIGSEGV
);
159 /* 2*oops - an invalid address and no processor */
164 /* got the parameters, allocate the space */
165 device_attach_address(device_parent(me
),
172 return block_nr_bytes
;
177 hw_vm_io_read_buffer_callback(device
*me
,
185 if (hw_vm_add_space(me
, addr
, nr_bytes
, processor
, cia
) >= nr_bytes
) {
186 memset(dest
, 0, nr_bytes
); /* always initialized to zero */
195 hw_vm_io_write_buffer_callback(device
*me
,
203 if (hw_vm_add_space(me
, addr
, nr_bytes
, processor
, cia
) >= nr_bytes
) {
204 return device_dma_write_buffer(device_parent(me
), source
,
207 0/*violate_read_only*/);
215 hw_vm_ioctl(device
*me
,
218 device_ioctl_request request
,
221 /* While the caller is notified that the heap has grown by the
222 requested amount, the heap is actually extended out to a page
224 hw_vm_device
*vm
= (hw_vm_device
*)device_data(me
);
226 case device_ioctl_break
:
228 unsigned_word requested_break
= va_arg(ap
, unsigned_word
);
229 unsigned_word new_break
= ALIGN_8(requested_break
);
230 unsigned_word old_break
= vm
->heap_bound
;
231 signed_word delta
= new_break
- old_break
;
233 vm
->heap_bound
= ALIGN_PAGE(new_break
);
237 device_error(me
, "Unsupported ioctl request");
245 static device_callbacks
const hw_vm_callbacks
= {
246 { hw_vm_init_address_callback
, },
247 { hw_vm_attach_address
,
248 passthrough_device_address_detach
, },
249 { hw_vm_io_read_buffer_callback
,
250 hw_vm_io_write_buffer_callback
, },
251 { NULL
, passthrough_device_dma_write_buffer
, },
252 { NULL
, }, /* interrupt */
253 { generic_device_unit_decode
,
254 generic_device_unit_encode
, },
261 hw_vm_create(const char *name
,
262 const device_unit
*address
,
265 hw_vm_device
*vm
= ZALLOC(hw_vm_device
);
269 const device_descriptor hw_vm_device_descriptor
[] = {
270 { "vm", hw_vm_create
, &hw_vm_callbacks
},
274 #endif /* _HW_VM_C_ */