/[gxemul]/trunk/src/devices/dev_8259.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/src/devices/dev_8259.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (hide annotations)
Mon Oct 8 16:18:38 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 7557 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.905 2005/08/16 09:16:24 debug Exp $
20050628	Continuing the work on the ARM translation engine. end_of_page
		works. Experimenting with load/store translation caches
		(virtual -> physical -> host).
20050629	More ARM stuff (memory access translation cache, mostly). This
		might break a lot of stuff elsewhere, probably some MIPS-
		related translation things.
20050630	Many load/stores are now automatically generated and included
		into cpu_arm_instr.c; 1024 functions in total (!).
		Fixes based on feedback from Alec Voropay: only print 8 hex
		digits instead of 16 in some cases when emulating 32-bit
		machines; similar 8 vs 16 digit fix for breakpoint addresses;
		4Kc has 16 TLB entries, not 48; the MIPS config select1
		register is now printed with "reg ,0".
		Also changing many other occurances of 16 vs 8 digit output.
		Adding cache associativity fields to mips_cpu_types.h; updating
		some other cache fields; making the output of
		mips_cpu_dumpinfo() look nicer.
		Generalizing the bintrans stuff for device accesses to also
		work with the new translation system. (This might also break
		some MIPS things.)
		Adding multi-load/store instructions to the ARM disassembler
		and the translator, and some optimizations of various kinds.
20050701	Adding a simple dev_disk (it can read/write sectors from
		disk images).
20050712	Adding dev_ether (a simple ethernet send/receive device).
		Debugger command "ninstrs" for toggling show_nr_of_instructions
		during runtime.
		Removing the framebuffer logo.
20050713	Continuing on dev_ether.
		Adding a dummy cpu_alpha (again).
20050714	More work on cpu_alpha.
20050715	More work on cpu_alpha. Many instructions work, enough to run
		a simple framebuffer fill test (similar to the ARM test).
20050716	More Alpha stuff.
20050717	Minor updates (Alpha stuff).
20050718	Minor updates (Alpha stuff).
20050719	Generalizing some Alpha instructions.
20050720	More Alpha-related updates.
20050721	Continuing on cpu_alpha. Importing rpb.h from NetBSD/alpha.
20050722	Alpha-related updates: userland stuff (Hello World using
		write() compiled statically for FreeBSD/Alpha runs fine), and
		more instructions are now implemented.
20050723	Fixing ldq_u and stq_u.
		Adding more instructions (conditional moves, masks, extracts,
		shifts).
20050724	More FreeBSD/Alpha userland stuff, and adding some more
		instructions (inserts).
20050725	Continuing on the Alpha stuff. (Adding dummy ldt/stt.)
		Adding a -A command line option to turn off alignment checks
		in some cases (for translated code).
		Trying to remove the old bintrans code which updated the pc
		and nr_of_executed_instructions for every instruction.
20050726	Making another attempt att removing the pc/nr of instructions
		code. This time it worked, huge performance increase for
		artificial test code, but performance loss for real-world
		code :-( so I'm scrapping that code for now.
		Tiny performance increase on Alpha (by using ret instead of
		jmp, to play nice with the Alpha's branch prediction) for the
		old MIPS bintrans backend.
20050727	Various minor fixes and cleanups.
20050728	Switching from a 2-level virtual to host/physical translation
		system for ARM emulation, to a 1-level translation.
		Trying to switch from 2-level to 1-level for the MIPS bintrans
		system as well (Alpha only, so far), but there is at least one
		problem: caches and/or how they work with device mappings.
20050730	Doing the 2-level to 1-level conversion for the i386 backend.
		The cache/device bug is still there for R2K/3K :(
		Various other minor updates (Malta etc).
		The mc146818 clock now updates the UIP bit in a way which works
		better with Linux for at least sgimips and Malta emulation.
		Beginning the work on refactoring the dyntrans system.
20050731	Continuing the dyntrans refactoring.
		Fixing a small but serious host alignment bug in memory_rw.
		Adding support for big-endian load/stores to the i386 bintrans
		backend.
		Another minor i386 bintrans backend update: stores from the
		zero register are now one (or two) loads shorter.
		The slt and sltu instructions were incorrectly implemented for
		the i386 backend; only using them for 32-bit mode for now.
20050801	Continuing the dyntrans refactoring.
		Cleanup of the ns16550 serial controller (removing unnecessary
		code).
		Bugfix (memory corruption bug) in dev_gt, and a patch/hack from
		Alec Voropay for Linux/Malta.
20050802	More cleanup/refactoring of the dyntrans subsystem: adding
		phys_page pointers to the lookup tables, for quick jumps
		between translated pages.
		Better fix for the ns16550 device (but still no real FIFO
		functionality).
		Converting cpu_ppc to the new dyntrans system. This means that
		I will have to start from scratch with implementing each
		instruction, and figure out how to implement dual 64/32-bit
		modes etc.
		Removing the URISC CPU family, because it was useless.
20050803	When selecting a machine type, the main type can now be omitted
		if the subtype name is unique. (I.e. -E can be omitted.)
		Fixing a dyntrans/device update bug. (Writes to offset 0 of
		a device could sometimes go unnoticed.)
		Adding an experimental "instruction combination" hack for
		ARM for memset-like byte fill loops.
20050804	Minor progress on cpu_alpha and related things.
		Finally fixing the MIPS dmult/dmultu bugs.
		Fixing some minor TODOs.
20050805	Generalizing the 8259 PIC. It now also works with Cobalt
		and evbmips emulation, in addition to the x86 hack.
		Finally converting the ns16550 device to use devinit.
		Continuing the work on the dyntrans system. Thinking about
		how to add breakpoints.
20050806	More dyntrans updates. Breakpoints seem to work now.
20050807	Minor updates: cpu_alpha and related things; removing
		dev_malta (as it isn't used any more).
		Dyntrans: working on general "show trace tree" support.
		The trace tree stuff now works with both the old MIPS code and
		with newer dyntrans modes. :)
		Continuing on Alpha-related stuff (trying to get *BSD to boot
		a bit further, adding more instructions, etc).
20050808	Adding a dummy IA64 cpu family, and continuing the refactoring
		of the dyntrans system.
		Removing the regression test stuff, because it was more or
		less useless.
		Adding loadlinked/storeconditional type instructions to the
		Alpha emulation. (Needed for Linux/alpha. Not very well tested
		yet.)
20050809	The function call trace tree now prints a per-function nr of
		arguments. (Semi-meaningless, since that data isn't read yet
		from the ELFs; some hardcoded symbols such as memcpy() and
		strlen() work fine, though.)
		More dyntrans refactoring; taking out more of the things that
		are common to all cpu families.
20050810	Working on adding support for "dual mode" for PPC dyntrans
		(i.e. both 64-bit and 32-bit modes).
		(Re)adding some simple PPC instructions.
20050811	Adding a dummy M68K cpu family. The dyntrans system isn't ready
		for variable-length ISAs yet, so it's completely bogus so far.
		Re-adding more PPC instructions.
		Adding a hack to src/file.c which allows OpenBSD/mac68k a.out
		kernels to be loaded.
		Beginning to add PPC loads/stores. So far they only work in
		32-bit mode.
20050812	The configure file option "add_remote" now accepts symbolic
		host names, in addition to numeric IPv4 addresses.
		Re-adding more PPC instructions.
20050814	Continuing to port back more PPC instructions.
		Found and fixed the cache/device write-update bug for 32-bit
		MIPS bintrans. :-)
		Triggered a really weird and annoying bug in Compaq's C
		compiler; ccc sometimes outputs code which loads from an
		address _before_ checking whether the pointer was NULL or not.
		(I'm not sure how to handle this problem.)
20050815	Removing all of the old x86 instruction execution code; adding
		a new (dummy) dyntrans module for x86.
		Taking the first steps to extend the dyntrans system to support
		variable-length instructions.
		Slowly preparing for the next release.
20050816	Adding a dummy SPARC cpu module.
		Minor updates (documentation etc) for the release.

==============  RELEASE 0.3.5  ==============


1 dpavlin 6 /*
2     * Copyright (C) 2005 Anders Gavare. All rights reserved.
3     *
4     * Redistribution and use in source and binary forms, with or without
5     * modification, are permitted provided that the following conditions are met:
6     *
7     * 1. Redistributions of source code must retain the above copyright
8     * notice, this list of conditions and the following disclaimer.
9     * 2. Redistributions in binary form must reproduce the above copyright
10     * notice, this list of conditions and the following disclaimer in the
11     * documentation and/or other materials provided with the distribution.
12     * 3. The name of the author may not be used to endorse or promote products
13     * derived from this software without specific prior written permission.
14     *
15     * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16     * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18     * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19     * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20     * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21     * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24     * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25     * SUCH DAMAGE.
26     *
27     *
28 dpavlin 12 * $Id: dev_8259.c,v 1.14 2005/08/05 09:08:20 debug Exp $
29 dpavlin 6 *
30     * 8259 Programmable Interrupt Controller.
31     *
32 dpavlin 12 * See the following URL for more details:
33 dpavlin 6 * http://www.nondot.org/sabre/os/files/MiscHW/8259pic.txt
34     */
35    
36     #include <stdio.h>
37     #include <stdlib.h>
38     #include <string.h>
39    
40     #include "cpu.h"
41     #include "device.h"
42     #include "devices.h"
43     #include "emul.h"
44     #include "machine.h"
45     #include "memory.h"
46     #include "misc.h"
47    
48    
49     #define DEV_8259_LENGTH 2
50    
51 dpavlin 12 /* #define DEV_8259_DEBUG */
52 dpavlin 6
53 dpavlin 12
54 dpavlin 6 /*
55     * dev_8259_access():
56     */
57     int dev_8259_access(struct cpu *cpu, struct memory *mem,
58     uint64_t relative_addr, unsigned char *data, size_t len,
59     int writeflag, void *extra)
60     {
61     struct pic8259_data *d = (struct pic8259_data *) extra;
62     uint64_t idata = 0, odata = 0;
63 dpavlin 12 int i;
64 dpavlin 6
65     idata = memory_readmax64(cpu, data, len);
66    
67 dpavlin 12 #ifdef DEV_8259_DEBUG
68     if (writeflag == MEM_READ)
69     fatal("[ 8259: read from 0x%x ]\n", (int)relative_addr);
70     else
71     fatal("[ 8259: write to 0x%x: 0x%x ]\n",
72     (int)relative_addr, (int)idata);
73     #endif
74    
75 dpavlin 6 switch (relative_addr) {
76     case 0x00:
77     if (writeflag == MEM_WRITE) {
78     if ((idata & 0x10) == 0x10) {
79     /* Bit 3: 0=edge, 1=level */
80     if (idata & 0x08)
81     fatal("[ 8259: TODO: Level "
82     "triggered (MCA bus) ]\n");
83     if (idata & 0x04)
84     fatal("[ 8259: WARNING: Bit 2 set ]\n");
85     /* Bit 1: 0=cascade, 1=single */
86     /* Bit 0: 1=4th init byte */
87 dpavlin 12 /* This happens on non-x86 systems:
88     if (!(idata & 0x01))
89 dpavlin 6 fatal("[ 8259: WARNING: Bit 0 NOT set!"
90 dpavlin 12 "!! ]\n"); */
91 dpavlin 6 d->init_state = 1;
92     break;
93     }
94    
95     /* TODO: Is it ok to abort init state when there
96     is a non-init command? */
97     if (d->init_state)
98     fatal("[ 8259: WARNING: Was in init-state, but"
99     " it was aborted? ]\n");
100     d->init_state = 0;
101    
102     switch (idata) {
103     case 0x0a:
104     d->current_command = 0x0a;
105     break;
106     case 0x0b:
107     d->current_command = 0x0b;
108     break;
109 dpavlin 12 case 0x0c:
110     /* Put Master in Buffered Mode */
111     d->current_command = 0x0c;
112     break;
113 dpavlin 6 case 0x20: /* End Of Interrupt */
114 dpavlin 12 /*
115     * TODO: in buffered mode, is this an EOI 0?
116     */
117 dpavlin 6 d->irr &= ~d->isr;
118     d->isr = 0;
119     /* Recalculate interrupt assertions: */
120 dpavlin 12 cpu_interrupt(cpu, d->irq_nr);
121 dpavlin 6 break;
122 dpavlin 12 case 0x21: /* Specific EOI */
123     case 0x22:
124     case 0x23:
125     case 0x24:
126     case 0x25:
127     case 0x26:
128     case 0x27:
129 dpavlin 6 case 0x60:
130     case 0x61:
131     case 0x62:
132     case 0x63:
133     case 0x64:
134     case 0x65:
135     case 0x66:
136     case 0x67: /* Specific EOI */
137     d->irr &= ~(1 << (idata & 7));
138     d->isr &= ~(1 << (idata & 7));
139     /* Recalculate interrupt assertions: */
140 dpavlin 12 cpu_interrupt(cpu, d->irq_nr);
141 dpavlin 6 break;
142     case 0x68: /* Set Special Mask Mode */
143     /* TODO */
144     break;
145     case 0xc0:
146     case 0xc1:
147     case 0xc2:
148     case 0xc3:
149     case 0xc4:
150     case 0xc5:
151     case 0xc6:
152     case 0xc7: /* Set IRQ Priority Order */
153     /* TODO */
154     break;
155     default:
156     fatal("[ 8259: unimplemented command 0x%02x"
157     " ]\n", idata);
158     cpu->running = 0;
159     }
160     } else {
161     switch (d->current_command) {
162     case 0x0a:
163     odata = d->irr;
164     break;
165     case 0x0b:
166     odata = d->isr;
167     break;
168 dpavlin 12 case 0x0c:
169     /* Buffered mode. */
170 dpavlin 6 default:
171 dpavlin 12 odata = 0x00;
172     for (i=0; i<8; i++)
173     if ((d->irr >> i) & 1) {
174     odata = 0x80 | i;
175     break;
176     }
177     break;
178     /*
179     * TODO: The "default" label should really do
180     * something like this:
181     *
182     * fatal("[ 8259: unimplemented command 0x%02x"
183     * " while reading ]\n", d->current_command);
184     * cpu->running = 0;
185     *
186     * but Linux seems to read from the secondary PIC
187     * in a manner which works better the way things
188     * are coded right now.
189     */
190 dpavlin 6 }
191     }
192     break;
193     case 0x01:
194     if (d->init_state > 0) {
195     if (d->init_state == 1) {
196     d->irq_base = idata & 0xf8;
197 dpavlin 12 /* This happens on non-x86 machines:
198     if (idata & 7)
199 dpavlin 6 fatal("[ 8259: WARNING! Lowest"
200     " bits in Init Cmd 1 are"
201 dpavlin 12 " non-zero! ]\n"); */
202 dpavlin 6 d->init_state = 2;
203     } else if (d->init_state == 2) {
204     /* Slave attachment. TODO */
205     d->init_state = 3;
206     } else if (d->init_state == 3) {
207     if (idata & 0x02)
208     fatal("[ 8259: WARNING! Bit 1 i"
209     "n Init Cmd 4 is set! ]\n");
210     if (!(idata & 0x01))
211     fatal("[ 8259: WARNING! Bit 0 "
212     "in Init Cmd 4 is not"
213     " set! ]\n");
214     d->init_state = 0;
215     }
216     break;
217     }
218    
219     if (writeflag == MEM_WRITE) {
220     d->ier = idata;
221     /* Recalculate interrupt assertions: */
222 dpavlin 12 cpu_interrupt(cpu, d->irq_nr);
223 dpavlin 6 } else {
224     odata = d->ier;
225     }
226     break;
227     default:
228     if (writeflag == MEM_WRITE) {
229     fatal("[ 8259: unimplemented write to address 0x%x"
230     " data=0x%02x ]\n", (int)relative_addr, (int)idata);
231     cpu->running = 0;
232     } else {
233     fatal("[ 8259: unimplemented read from address 0x%x "
234     "]\n", (int)relative_addr);
235     cpu->running = 0;
236     }
237     }
238    
239     if (writeflag == MEM_READ)
240     memory_writemax64(cpu, data, len, odata);
241    
242     return 1;
243     }
244    
245    
246     /*
247     * devinit_8259():
248 dpavlin 12 *
249     * Initialize an 8259 PIC. Important notes:
250     *
251     * x) Most systems use _TWO_ 8259 PICs. These should be registered
252     * as separate devices.
253     *
254     * x) The irq number specified is the number used to re-calculate
255     * CPU interrupt assertions. It is _not_ the irq number at
256     * which the PIC is connected. (That is left to machine specific
257     * code in src/machine.c.)
258 dpavlin 6 */
259     int devinit_8259(struct devinit *devinit)
260     {
261     struct pic8259_data *d = malloc(sizeof(struct pic8259_data));
262     char *name2;
263 dpavlin 12 size_t nlen = strlen(devinit->name) + 20;
264 dpavlin 6
265     if (d == NULL) {
266     fprintf(stderr, "out of memory\n");
267     exit(1);
268     }
269     memset(d, 0, sizeof(struct pic8259_data));
270     d->irq_nr = devinit->irq_nr;
271    
272     name2 = malloc(nlen);
273     snprintf(name2, nlen, "%s", devinit->name);
274 dpavlin 12 if ((devinit->addr & 0xfff) == 0xa0) {
275 dpavlin 10 strlcat(name2, " [secondary]", nlen);
276 dpavlin 12 d->irq_base = 8;
277     }
278 dpavlin 6
279     memory_device_register(devinit->machine->memory, name2,
280 dpavlin 12 devinit->addr, DEV_8259_LENGTH, dev_8259_access, d,
281 dpavlin 6 MEM_DEFAULT, NULL);
282    
283     devinit->return_ptr = d;
284     return 1;
285     }
286    

  ViewVC Help
Powered by ViewVC 1.1.26