Update
[gdb.git] / sim / ppc / cpu.c
blobbce82f1d1410f6f9041deabe38b49524fbe24369
1 /* This file is part of the program psim.
3 Copyright (C) 1994-1997, 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 /* the system this processor is contained within */
53 cpu_mon *monitor;
54 os_emul *os_emulation;
55 psim *system;
56 event_queue *events;
57 int cpu_nr;
59 /* Current CPU model information */
60 model_data *model_ptr;
62 #if WITH_IDECODE_CACHE_SIZE
63 /* a cache to store cracked instructions */
64 idecode_cache icache[WITH_IDECODE_CACHE_SIZE];
65 #endif
67 /* any interrupt state */
68 interrupts ints;
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 cpu_mon *monitor,
86 os_emul *os_emulation,
87 int cpu_nr)
89 cpu *processor = ZALLOC(cpu);
91 /* create the virtual memory map from the core */
92 processor->physical = memory;
93 processor->virtual = vm_create(memory);
94 processor->instruction_map = vm_create_instruction_map(processor->virtual);
95 processor->data_map = vm_create_data_map(processor->virtual);
97 if (CURRENT_MODEL_ISSUE > 0)
98 processor->model_ptr = model_create (processor);
100 /* link back to core system */
101 processor->system = system;
102 processor->events = psim_event_queue(system);
103 processor->cpu_nr = cpu_nr;
104 processor->monitor = monitor;
105 processor->os_emulation = os_emulation;
107 return processor;
111 INLINE_CPU\
112 (void)
113 cpu_init(cpu *processor)
115 memset(&processor->regs, 0, sizeof(processor->regs));
116 /* vm init is delayed until after the device tree has been init as
117 the devices may further init the cpu */
118 if (CURRENT_MODEL_ISSUE > 0)
119 model_init (processor->model_ptr);
123 /* find ones way home */
125 INLINE_CPU\
126 (psim *)
127 cpu_system(cpu *processor)
129 return processor->system;
132 INLINE_CPU\
133 (int)
134 cpu_nr(cpu *processor)
136 return processor->cpu_nr;
139 INLINE_CPU\
140 (cpu_mon *)
141 cpu_monitor(cpu *processor)
143 return processor->monitor;
146 INLINE_CPU\
147 (os_emul *)
148 cpu_os_emulation(cpu *processor)
150 return processor->os_emulation;
153 INLINE_CPU\
154 (model_data *)
155 cpu_model(cpu *processor)
157 return processor->model_ptr;
161 /* program counter manipulation */
163 INLINE_CPU\
164 (void)
165 cpu_set_program_counter(cpu *processor,
166 unsigned_word new_program_counter)
168 processor->program_counter = new_program_counter;
171 INLINE_CPU\
172 (unsigned_word)
173 cpu_get_program_counter(cpu *processor)
175 return processor->program_counter;
179 INLINE_CPU\
180 (void)
181 cpu_restart(cpu *processor,
182 unsigned_word nia)
184 ASSERT(processor != NULL);
185 cpu_set_program_counter(processor, nia);
186 psim_restart(processor->system, processor->cpu_nr);
189 INLINE_CPU\
190 (void)
191 cpu_halt(cpu *processor,
192 unsigned_word nia,
193 stop_reason reason,
194 int signal)
196 ASSERT(processor != NULL);
197 if (CURRENT_MODEL_ISSUE > 0)
198 model_halt(processor->model_ptr);
199 cpu_set_program_counter(processor, nia);
200 psim_halt(processor->system, processor->cpu_nr, reason, signal);
203 EXTERN_CPU\
204 (void)
205 cpu_error(cpu *processor,
206 unsigned_word cia,
207 const char *fmt,
208 ...)
210 char message[1024];
211 va_list ap;
213 /* format the message */
214 va_start(ap, fmt);
215 vsprintf(message, fmt, ap);
216 va_end(ap);
218 /* sanity check */
219 if (strlen(message) >= sizeof(message))
220 error("cpu_error: buffer overflow");
222 if (processor != NULL) {
223 printf_filtered("cpu %d, cia 0x%lx: %s\n",
224 processor->cpu_nr + 1, (unsigned long)cia, message);
225 cpu_halt(processor, cia, was_signalled, -1);
227 else {
228 error("cpu: %s", message);
233 /* The processors local concept of time */
235 INLINE_CPU\
236 (signed64)
237 cpu_get_time_base(cpu *processor)
239 return (event_queue_time(processor->events)
240 - processor->time_base_local_time);
243 INLINE_CPU\
244 (void)
245 cpu_set_time_base(cpu *processor,
246 signed64 time_base)
248 processor->time_base_local_time = (event_queue_time(processor->events)
249 - time_base);
252 INLINE_CPU\
253 (signed32)
254 cpu_get_decrementer(cpu *processor)
256 return (processor->decrementer_local_time
257 - event_queue_time(processor->events));
260 STATIC_INLINE_CPU\
261 (void)
262 cpu_decrement_event(void *data)
264 cpu *processor = (cpu*)data;
265 processor->decrementer_event = NULL;
266 decrementer_interrupt(processor);
269 INLINE_CPU\
270 (void)
271 cpu_set_decrementer(cpu *processor,
272 signed32 decrementer)
274 signed64 old_decrementer = cpu_get_decrementer(processor);
275 event_queue_deschedule(processor->events, processor->decrementer_event);
276 processor->decrementer_event = NULL;
277 processor->decrementer_local_time = (event_queue_time(processor->events)
278 + decrementer);
279 if (decrementer < 0 && old_decrementer >= 0)
280 /* A decrementer interrupt occures if the sign of the decrement
281 register is changed from positive to negative by the load
282 instruction */
283 decrementer_interrupt(processor);
284 else if (decrementer >= 0)
285 processor->decrementer_event = event_queue_schedule(processor->events,
286 decrementer,
287 cpu_decrement_event,
288 processor);
292 #if WITH_IDECODE_CACHE_SIZE
293 /* allow access to the cpu's instruction cache */
294 INLINE_CPU\
295 (idecode_cache *)
296 cpu_icache_entry(cpu *processor,
297 unsigned_word cia)
299 return &processor->icache[cia / 4 % WITH_IDECODE_CACHE_SIZE];
303 INLINE_CPU\
304 (void)
305 cpu_flush_icache(cpu *processor)
307 int i;
308 /* force all addresses to 0xff... so that they never hit */
309 for (i = 0; i < WITH_IDECODE_CACHE_SIZE; i++)
310 processor->icache[i].address = MASK(0, 63);
312 #endif
315 /* address map revelation */
317 INLINE_CPU\
318 (vm_instruction_map *)
319 cpu_instruction_map(cpu *processor)
321 return processor->instruction_map;
324 INLINE_CPU\
325 (vm_data_map *)
326 cpu_data_map(cpu *processor)
328 return processor->data_map;
331 INLINE_CPU\
332 (void)
333 cpu_page_tlb_invalidate_entry(cpu *processor,
334 unsigned_word ea)
336 vm_page_tlb_invalidate_entry(processor->virtual, ea);
339 INLINE_CPU\
340 (void)
341 cpu_page_tlb_invalidate_all(cpu *processor)
343 vm_page_tlb_invalidate_all(processor->virtual);
347 /* interrupt access */
349 INLINE_CPU\
350 (interrupts *)
351 cpu_interrupts(cpu *processor)
353 return &processor->ints;
358 /* reservation access */
360 INLINE_CPU\
361 (memory_reservation *)
362 cpu_reservation(cpu *processor)
364 return &processor->reservation;
368 /* register access */
370 INLINE_CPU\
371 (registers *)
372 cpu_registers(cpu *processor)
374 return &processor->regs;
377 INLINE_CPU\
378 (void)
379 cpu_synchronize_context(cpu *processor,
380 unsigned_word cia)
382 #if (WITH_IDECODE_CACHE_SIZE)
383 /* kill of the cache */
384 cpu_flush_icache(processor);
385 #endif
387 /* update virtual memory */
388 vm_synchronize_context(processor->virtual,
389 processor->regs.spr,
390 processor->regs.sr,
391 processor->regs.msr,
392 processor, cia);
396 /* might again be useful one day */
398 INLINE_CPU\
399 (void)
400 cpu_print_info(cpu *processor, int verbose)
404 #endif /* _CPU_C_ */