* Test case for PR 18452.
[binutils-gdb.git] / sim / ppc / cpu.c
blob9e9e7e7330d15d5876d0b9797a25ec396317291d
1 /* This file is part of the program psim.
3 Copyright (C) 1994-1995, 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.
22 #ifndef _CPU_C_
23 #define _CPU_C_
25 #include <setjmp.h>
27 #include "cpu.h"
28 #include "idecode.h"
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #else
33 #ifdef HAVE_STRINGS_H
34 #include <strings.h>
35 #endif
36 #endif
38 struct _cpu {
40 /* the registers */
41 registers regs;
43 /* current instruction address */
44 unsigned_word program_counter;
46 /* the memory maps */
47 core *physical; /* all of memory */
48 vm *virtual;
49 vm_instruction_map *instruction_map; /* instructions */
50 vm_data_map *data_map; /* data */
52 /* current state of interrupt inputs */
53 int external_exception_pending;
55 /* the system this processor is contained within */
56 cpu_mon *monitor;
57 os_emul *os_emulation;
58 psim *system;
59 event_queue *events;
60 int cpu_nr;
62 /* Current CPU model information */
63 model_data *model_ptr;
65 #if WITH_IDECODE_CACHE_SIZE
66 /* a cache to store cracked instructions */
67 idecode_cache icache[WITH_IDECODE_CACHE_SIZE];
68 #endif
70 /* address reservation: keep the physical address and the contents
71 of memory at that address */
72 memory_reservation reservation;
74 /* offset from event time to this cpu's idea of the local time */
75 signed64 time_base_local_time;
76 signed64 decrementer_local_time;
77 event_entry_tag decrementer_event;
81 INLINE_CPU\
82 (cpu *)
83 cpu_create(psim *system,
84 core *memory,
85 event_queue *events,
86 cpu_mon *monitor,
87 os_emul *os_emulation,
88 int cpu_nr)
90 cpu *processor = ZALLOC(cpu);
92 /* create the virtual memory map from the core */
93 processor->physical = memory;
94 processor->virtual = vm_create(memory);
95 processor->instruction_map = vm_create_instruction_map(processor->virtual);
96 processor->data_map = vm_create_data_map(processor->virtual);
98 if (CURRENT_MODEL_ISSUE > 0)
99 processor->model_ptr = model_create (processor);
101 /* link back to core system */
102 processor->system = system;
103 processor->events = events;
104 processor->cpu_nr = cpu_nr;
105 processor->monitor = monitor;
106 processor->os_emulation = os_emulation;
108 return processor;
112 INLINE_CPU\
113 (void)
114 cpu_init(cpu *processor)
116 memset(&processor->regs, 0, sizeof(processor->regs));
117 /* vm init is delayed until after the device tree has been init as
118 the devices may further init the cpu */
119 if (CURRENT_MODEL_ISSUE > 0)
120 model_init (processor->model_ptr);
124 /* find ones way home */
126 INLINE_CPU\
127 (psim *)
128 cpu_system(cpu *processor)
130 return processor->system;
133 INLINE_CPU\
134 (int)
135 cpu_nr(cpu *processor)
137 return processor->cpu_nr;
140 INLINE_CPU\
141 (event_queue *)
142 cpu_event_queue(cpu *processor)
144 return processor->events;
147 INLINE_CPU\
148 (cpu_mon *)
149 cpu_monitor(cpu *processor)
151 return processor->monitor;
154 INLINE_CPU\
155 (os_emul *)
156 cpu_os_emulation(cpu *processor)
158 return processor->os_emulation;
161 INLINE_CPU\
162 (model_data *)
163 cpu_model(cpu *processor)
165 return processor->model_ptr;
168 /* The processors local concept of time */
170 INLINE_CPU\
171 (signed64)
172 cpu_get_time_base(cpu *processor)
174 return (event_queue_time(processor->events)
175 - processor->time_base_local_time);
178 INLINE_CPU\
179 (void)
180 cpu_set_time_base(cpu *processor,
181 signed64 time_base)
183 processor->time_base_local_time = (event_queue_time(processor->events)
184 - time_base);
187 INLINE_CPU\
188 (signed32)
189 cpu_get_decrementer(cpu *processor)
191 return (processor->decrementer_local_time
192 - event_queue_time(processor->events));
195 STATIC_INLINE_CPU\
196 (void)
197 cpu_decrement_event(event_queue *queue,
198 void *data)
200 cpu *processor = (cpu*)data;
201 if (!decrementer_interrupt(processor)) {
202 processor->decrementer_event = event_queue_schedule(processor->events,
203 1, /* NOW! */
204 cpu_decrement_event,
205 processor);
209 INLINE_CPU\
210 (void)
211 cpu_set_decrementer(cpu *processor,
212 signed32 decrementer)
214 signed64 old_decrementer = (processor->decrementer_local_time
215 - event_queue_time(processor->events));
216 event_queue_deschedule(processor->events, processor->decrementer_event);
217 processor->decrementer_local_time = (event_queue_time(processor->events)
218 + decrementer);
219 if (decrementer < 0 && old_decrementer >= 0)
220 /* dec interrupt occures if the sign of the decrement reg is
221 changed by the load operation */
222 processor->decrementer_event = event_queue_schedule(processor->events,
223 1, /* NOW! */
224 cpu_decrement_event,
225 processor);
226 else if (decrementer >= 0)
227 processor->decrementer_event = event_queue_schedule(processor->events,
228 decrementer,
229 cpu_decrement_event,
230 processor);
234 /* program counter manipulation */
236 INLINE_CPU\
237 (void)
238 cpu_set_program_counter(cpu *processor,
239 unsigned_word new_program_counter)
241 processor->program_counter = new_program_counter;
244 INLINE_CPU\
245 (unsigned_word)
246 cpu_get_program_counter(cpu *processor)
248 return processor->program_counter;
251 INLINE_CPU\
252 (void)
253 cpu_restart(cpu *processor,
254 unsigned_word nia)
256 processor->program_counter = nia;
257 psim_restart(processor->system, processor->cpu_nr);
260 INLINE_CPU\
261 (void)
262 cpu_halt(cpu *processor,
263 unsigned_word cia,
264 stop_reason reason,
265 int signal)
267 if (processor == NULL) {
268 error("cpu_halt() processor=NULL, cia=0x%x, reason=%d, signal=%d\n",
269 cia,
270 reason,
271 signal);
273 else {
274 if (CURRENT_MODEL_ISSUE > 0)
275 model_halt(processor->model_ptr);
277 processor->program_counter = cia;
278 psim_halt(processor->system, processor->cpu_nr, cia, reason, signal);
283 #if WITH_IDECODE_CACHE_SIZE
284 /* allow access to the cpu's instruction cache */
285 INLINE_CPU\
286 (idecode_cache *)
287 cpu_icache_entry(cpu *processor,
288 unsigned_word cia)
290 return &processor->icache[cia / 4 % WITH_IDECODE_CACHE_SIZE];
294 INLINE_CPU\
295 (void)
296 cpu_flush_icache(cpu *processor)
298 int i;
299 /* force all addresses to 0xff... so that they never hit */
300 for (i = 0; i < WITH_IDECODE_CACHE_SIZE; i++)
301 processor->icache[i].address = MASK(0, 63);
303 #endif
306 /* address map revelation */
308 INLINE_CPU\
309 (vm_instruction_map *)
310 cpu_instruction_map(cpu *processor)
312 return processor->instruction_map;
315 INLINE_CPU\
316 (vm_data_map *)
317 cpu_data_map(cpu *processor)
319 return processor->data_map;
322 INLINE_CPU\
323 (void)
324 cpu_page_tlb_invalidate_entry(cpu *processor,
325 unsigned_word ea)
327 vm_page_tlb_invalidate_entry(processor->virtual, ea);
330 INLINE_CPU\
331 (void)
332 cpu_page_tlb_invalidate_all(cpu *processor)
334 vm_page_tlb_invalidate_all(processor->virtual);
338 /* reservation access */
340 INLINE_CPU\
341 (memory_reservation *)
342 cpu_reservation(cpu *processor)
344 return &processor->reservation;
348 /* register access */
350 INLINE_CPU\
351 (registers *)
352 cpu_registers(cpu *processor)
354 return &processor->regs;
357 INLINE_CPU\
358 (void)
359 cpu_synchronize_context(cpu *processor)
361 #if (WITH_IDECODE_CACHE)
362 /* kill of the cache */
363 cpu_flush_icache(processor);
364 #endif
366 /* update virtual memory */
367 vm_synchronize_context(processor->virtual,
368 processor->regs.spr,
369 processor->regs.sr,
370 processor->regs.msr);
374 /* might again be useful one day */
376 INLINE_CPU\
377 (void)
378 cpu_print_info(cpu *processor, int verbose)
382 #endif /* _CPU_C_ */