137985Sphk/*-
237985Sphk * Copyright (c) 2004 Marcel Moolenaar
337985Sphk * All rights reserved.
437985Sphk *
537985Sphk * Redistribution and use in source and binary forms, with or without
637985Sphk * modification, are permitted provided that the following conditions
737985Sphk * are met:
837985Sphk *
937985Sphk * 1. Redistributions of source code must retain the above copyright
1037985Sphk *    notice, this list of conditions and the following disclaimer.
1137985Sphk * 2. Redistributions in binary form must reproduce the above copyright
1237985Sphk *    notice, this list of conditions and the following disclaimer in the
1337985Sphk *    documentation and/or other materials provided with the distribution.
1437985Sphk *
1537985Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
1637985Sphk * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1737985Sphk * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1837985Sphk * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
1937985Sphk * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2037985Sphk * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2137985Sphk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2237985Sphk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2337985Sphk * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2437985Sphk * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2537985Sphk */
2637985Sphk
2737985Sphk#include <sys/cdefs.h>
2850476Speter__FBSDID("$FreeBSD$");
2948794Snik
30155750Sbrd#include <sys/param.h>
3137985Sphk#include <sys/systm.h>
3279531Sru#include <sys/kdb.h>
3337985Sphk#include <sys/kernel.h>
3437985Sphk#include <sys/proc.h>
3537985Sphk#include <sys/signal.h>
3659501Sphantom
37124535Sru#include <machine/gdb_machdep.h>
3837985Sphk#include <machine/md_var.h>
3984306Sru#include <machine/pcb.h>
4037985Sphk#include <machine/reg.h>
4137985Sphk
4237985Sphk#include <gdb/gdb.h>
4337985Sphk#include <gdb/gdb_int.h>
4437985Sphk
4537985Sphkvoid *
4637985Sphkgdb_cpu_getreg(int regnum, size_t *regsz)
4737985Sphk{
48155750Sbrd	static uint64_t synth;
4937985Sphk	uint64_t cfm;
5037985Sphk
5171895Sru	*regsz = gdb_cpu_regsz(regnum);
5271895Sru	switch (regnum) {
5337985Sphk	/* Registers 0-127: general registers. */
5471919Snik	case 1:  return (&kdb_thrctx->pcb_special.gp);
5537985Sphk	case 4:  return (&kdb_thrctx->pcb_preserved.gr4);
5637985Sphk	case 5:  return (&kdb_thrctx->pcb_preserved.gr5);
5737985Sphk	case 6:  return (&kdb_thrctx->pcb_preserved.gr6);
5837985Sphk	case 7:  return (&kdb_thrctx->pcb_preserved.gr7);
5937985Sphk	case 12: return (&kdb_thrctx->pcb_special.sp);
6057686Ssheldonh	case 13: return (&kdb_thrctx->pcb_special.tp);
6157686Ssheldonh	/* Registers 128-255: floating-point registers. */
6237985Sphk	case 130: return (&kdb_thrctx->pcb_preserved_fp.fr2);
6337985Sphk	case 131: return (&kdb_thrctx->pcb_preserved_fp.fr3);
64110442Scharnier	case 132: return (&kdb_thrctx->pcb_preserved_fp.fr4);
6537985Sphk	case 133: return (&kdb_thrctx->pcb_preserved_fp.fr5);
66110442Scharnier	case 144: return (&kdb_thrctx->pcb_preserved_fp.fr16);
6737985Sphk	case 145: return (&kdb_thrctx->pcb_preserved_fp.fr17);
6837985Sphk	case 146: return (&kdb_thrctx->pcb_preserved_fp.fr18);
6937985Sphk	case 147: return (&kdb_thrctx->pcb_preserved_fp.fr19);
7037985Sphk	case 148: return (&kdb_thrctx->pcb_preserved_fp.fr20);
7137985Sphk	case 149: return (&kdb_thrctx->pcb_preserved_fp.fr21);
7250076Srse	case 150: return (&kdb_thrctx->pcb_preserved_fp.fr22);
7337985Sphk	case 151: return (&kdb_thrctx->pcb_preserved_fp.fr23);
74168038Simp	case 152: return (&kdb_thrctx->pcb_preserved_fp.fr24);
75233648Seadler	case 153: return (&kdb_thrctx->pcb_preserved_fp.fr25);
76233648Seadler	case 154: return (&kdb_thrctx->pcb_preserved_fp.fr26);
77168038Simp	case 155: return (&kdb_thrctx->pcb_preserved_fp.fr27);
7837985Sphk	case 156: return (&kdb_thrctx->pcb_preserved_fp.fr28);
7937985Sphk	case 157: return (&kdb_thrctx->pcb_preserved_fp.fr29);
8038702Swosch	case 158: return (&kdb_thrctx->pcb_preserved_fp.fr30);
8138702Swosch	case 159: return (&kdb_thrctx->pcb_preserved_fp.fr31);
8237985Sphk	/* Registers 320-327: branch registers. */
8337985Sphk	case 320:
8438702Swosch		if (kdb_thrctx->pcb_special.__spare == ~0UL)
8537985Sphk			return (&kdb_thrctx->pcb_special.rp);
86110442Scharnier		break;
8737985Sphk	case 321: return (&kdb_thrctx->pcb_preserved.br1);
88110442Scharnier	case 322: return (&kdb_thrctx->pcb_preserved.br2);
8973093Sru	case 323: return (&kdb_thrctx->pcb_preserved.br3);
90	case 324: return (&kdb_thrctx->pcb_preserved.br4);
91	case 325: return (&kdb_thrctx->pcb_preserved.br5);
92	/* Registers 328-333: misc. other registers. */
93	case 330: return (&kdb_thrctx->pcb_special.pr);
94	case 331:
95		if (kdb_thrctx->pcb_special.__spare == ~0UL) {
96			synth = kdb_thrctx->pcb_special.iip;
97			synth += (kdb_thrctx->pcb_special.psr >> 41) & 3;
98			return (&synth);
99		}
100		return (&kdb_thrctx->pcb_special.rp);
101	case 333:
102		if (kdb_thrctx->pcb_special.__spare == ~0UL)
103			return (&kdb_thrctx->pcb_special.cfm);
104		return (&kdb_thrctx->pcb_special.pfs);
105	/* Registers 334-461: application registers. */
106	case 350: return (&kdb_thrctx->pcb_special.rsc);
107	case 351: /* bsp */
108	case 352: /* bspstore. */
109		synth = kdb_thrctx->pcb_special.bspstore;
110		if (kdb_thrctx->pcb_special.__spare == ~0UL) {
111			synth += kdb_thrctx->pcb_special.ndirty;
112		} else {
113			cfm = kdb_thrctx->pcb_special.pfs;
114			synth = ia64_bsp_adjust(synth,
115			    IA64_CFM_SOF(cfm) - IA64_CFM_SOL(cfm));
116		}
117		return (&synth);
118	case 353: return (&kdb_thrctx->pcb_special.rnat);
119	case 370: return (&kdb_thrctx->pcb_special.unat);
120	case 374: return (&kdb_thrctx->pcb_special.fpsr);
121	case 398:
122		if (kdb_thrctx->pcb_special.__spare == ~0UL)
123			return (&kdb_thrctx->pcb_special.pfs);
124		break;
125	case 399: return (&kdb_thrctx->pcb_preserved.lc);
126	}
127	return (NULL);
128}
129
130void
131gdb_cpu_setreg(int regnum, void *val)
132{
133
134	switch (regnum) {
135	case GDB_REG_PC: break;
136	}
137}
138
139int
140gdb_cpu_signal(int vector, int dummy __unused)
141{
142
143	if (vector == IA64_VEC_BREAK || vector == IA64_VEC_SINGLE_STEP_TRAP)
144		return (SIGTRAP);
145	/* Add 100 so GDB won't translate the vector into signal names. */
146	return (vector + 100);
147}
148
149int
150gdb_cpu_query(void)
151{
152#if 0
153	uint64_t bspstore, *kstack;
154#endif
155	uintmax_t slot;
156
157	if (!gdb_rx_equal("Part:dirty:read::"))
158		return (0);
159
160	if (gdb_rx_varhex(&slot) < 0) {
161		gdb_tx_err(EINVAL);
162		return (-1);
163	}
164
165	gdb_tx_err(EINVAL);
166	return (-1);
167
168#if 0
169	/* slot is unsigned. No need to test for negative values. */
170	if (slot >= (kdb_frame->tf_special.ndirty >> 3)) {
171		return (-1);
172	}
173
174	/*
175	 * If the trapframe describes a kernel entry, bspstore holds
176	 * the address of the user backing store. Calculate the right
177	 * kernel stack address. See also ptrace_machdep().
178	 */
179	bspstore = kdb_frame->tf_special.bspstore;
180	kstack = (bspstore >= VM_MAXUSER_ADDRESS) ? (uint64_t*)bspstore :
181	    (uint64_t*)(kdb_thread->td_kstack + (bspstore & 0x1ffUL));
182	gdb_tx_begin('\0');
183	gdb_tx_mem((void*)(kstack + slot), 8);
184	gdb_tx_end();
185	return (1);
186#endif
187}
188