xref: /trueos/gnu/usr.bin/gdb/kgdb/trgt_powerpc64.c (revision 69d25b60d5de991a083072a8f88527e258c12e91)
1 /*-
2  * Copyright (c) 2006 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include <machine/pcb.h>
32 #include <machine/frame.h>
33 #include <err.h>
34 #include <kvm.h>
35 #include <string.h>
36 
37 #include <defs.h>
38 #include <target.h>
39 #include <gdbthread.h>
40 #include <inferior.h>
41 #include <regcache.h>
42 #include <frame-unwind.h>
43 #include <ppc-tdep.h>
44 
45 #include "kgdb.h"
46 
47 CORE_ADDR
kgdb_trgt_core_pcb(u_int cpuid)48 kgdb_trgt_core_pcb(u_int cpuid)
49 {
50 	return (kgdb_trgt_stop_pcb(cpuid, sizeof(struct pcb)));
51 }
52 
53 void
kgdb_trgt_fetch_registers(int regno __unused)54 kgdb_trgt_fetch_registers(int regno __unused)
55 {
56 	struct kthr *kt;
57 	struct pcb pcb;
58 	struct gdbarch_tdep *tdep;
59 	int i;
60 
61 	tdep = gdbarch_tdep (current_gdbarch);
62 
63 	kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
64 	if (kt == NULL)
65 		return;
66 	if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
67 		warnx("kvm_read: %s", kvm_geterr(kvm));
68 		memset(&pcb, 0, sizeof(pcb));
69 	}
70 
71 	/*
72 	 * r14-r31 are saved in the pcb
73 	 */
74 	for (i = 14; i <= 31; i++) {
75 		supply_register(tdep->ppc_gp0_regnum + i,
76 		    (char *)&pcb.pcb_context[i]);
77 	}
78 
79 	/* r1 is saved in the sp field */
80 	supply_register(tdep->ppc_gp0_regnum + 1, (char *)&pcb.pcb_sp);
81 	/* r2 is saved in the toc field */
82 	supply_register(tdep->ppc_gp0_regnum + 2, (char *)&pcb.pcb_toc);
83 
84 	supply_register(tdep->ppc_lr_regnum, (char *)&pcb.pcb_lr);
85 	supply_register(tdep->ppc_cr_regnum, (char *)&pcb.pcb_cr);
86 }
87 
88 void
kgdb_trgt_store_registers(int regno __unused)89 kgdb_trgt_store_registers(int regno __unused)
90 {
91 	fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
92 }
93 
94 void
kgdb_trgt_new_objfile(struct objfile * objfile)95 kgdb_trgt_new_objfile(struct objfile *objfile)
96 {
97 }
98 
99 struct kgdb_frame_cache {
100 	CORE_ADDR	pc;
101 	CORE_ADDR	sp;
102 };
103 
104 static struct kgdb_frame_cache *
kgdb_trgt_frame_cache(struct frame_info * next_frame,void ** this_cache)105 kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
106 {
107 	char buf[MAX_REGISTER_SIZE];
108 	struct kgdb_frame_cache *cache;
109 
110 	cache = *this_cache;
111 	if (cache == NULL) {
112 		cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
113 		*this_cache = cache;
114 		cache->pc = frame_func_unwind(next_frame);
115 		frame_unwind_register(next_frame, SP_REGNUM, buf);
116 		cache->sp = extract_unsigned_integer(buf,
117 		    register_size(current_gdbarch, SP_REGNUM));
118 	}
119 	return (cache);
120 }
121 
122 static void
kgdb_trgt_trapframe_this_id(struct frame_info * next_frame,void ** this_cache,struct frame_id * this_id)123 kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
124     struct frame_id *this_id)
125 {
126 	struct kgdb_frame_cache *cache;
127 
128 	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
129 	*this_id = frame_id_build(cache->sp, cache->pc);
130 }
131 
132 static void
kgdb_trgt_trapframe_prev_register(struct frame_info * next_frame,void ** this_cache,int regnum,int * optimizedp,enum lval_type * lvalp,CORE_ADDR * addrp,int * realnump,void * valuep)133 kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
134     void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
135     CORE_ADDR *addrp, int *realnump, void *valuep)
136 {
137 	char dummy_valuep[MAX_REGISTER_SIZE];
138 	struct gdbarch_tdep *tdep;
139 	struct kgdb_frame_cache *cache;
140 	int ofs, regsz;
141 
142 	tdep = gdbarch_tdep(current_gdbarch);
143 	regsz = register_size(current_gdbarch, regnum);
144 
145 	if (valuep == NULL)
146 		valuep = dummy_valuep;
147 	memset(valuep, 0, regsz);
148 	*optimizedp = 0;
149 	*addrp = 0;
150 	*lvalp = not_lval;
151 	*realnump = -1;
152 
153 	if (regnum >= tdep->ppc_gp0_regnum &&
154 	    regnum <= tdep->ppc_gplast_regnum)
155 		ofs = offsetof(struct trapframe,
156 		    fixreg[regnum - tdep->ppc_gp0_regnum]);
157 	else if (regnum == tdep->ppc_lr_regnum)
158 		ofs = offsetof(struct trapframe, lr);
159 	else if (regnum == tdep->ppc_cr_regnum)
160 		ofs = offsetof(struct trapframe, cr);
161 	else if (regnum == tdep->ppc_xer_regnum)
162 		ofs = offsetof(struct trapframe, xer);
163 	else if (regnum == tdep->ppc_ctr_regnum)
164 		ofs = offsetof(struct trapframe, ctr);
165 	else if (regnum == PC_REGNUM)
166 		ofs = offsetof(struct trapframe, srr0);
167 	else
168 		return;
169 
170 	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
171 	*addrp = cache->sp + 48 + ofs;
172 	*lvalp = lval_memory;
173 	target_read_memory(*addrp, valuep, regsz);
174 }
175 
176 static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
177         UNKNOWN_FRAME,
178         &kgdb_trgt_trapframe_this_id,
179         &kgdb_trgt_trapframe_prev_register
180 };
181 
182 const struct frame_unwind *
kgdb_trgt_trapframe_sniffer(struct frame_info * next_frame)183 kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
184 {
185 	char *pname;
186 	CORE_ADDR pc;
187 
188 	pc = frame_pc_unwind(next_frame);
189 	pname = NULL;
190 	find_pc_partial_function(pc, &pname, NULL, NULL);
191 	if (pname == NULL)
192 		return (NULL);
193 	if (strcmp(pname, "asttrapexit") == 0 ||
194 	    strcmp(pname, "trapexit") == 0)
195 		return (&kgdb_trgt_trapframe_unwind);
196 	/* printf("%s: %llx =%s\n", __func__, pc, pname); */
197 	return (NULL);
198 }
199