2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
28 * Author: David B. Golub, Carnegie Mellon University
38 * Multiple symbol tables
41 #define MAXNOSYMTABS 3 /* mach, ux, emulator */
44 unsigned int db_maxoff
= 0x10000;
45 unsigned long modAddr
= 0;
47 /* NWT: fault injection routine only.
48 * figure out start of function address given an address (off) in kernel text.
49 * name = function name
50 * value = function address
51 * d = difference between off and function address
52 * input is the desired address off and fault type
53 * returns closest instruction address (if found), NULL otherwise
56 find_faulty_instr(db_expr_t off
, int type
, int *instr_len
)
60 db_expr_t value
, cur_value
, prev_value
= 0;
61 int verbose
=0, found
=0;
62 const char * mod_name
= NULL
;
63 unsigned long mod_start
;
64 unsigned long mod_end
;
65 const char * sec_name
= NULL
;
66 unsigned long sec_start
;
67 unsigned long sec_end
;
68 const char * sym_name
= NULL
;
69 unsigned long sym_start
;
70 unsigned long sym_end
;
74 if (kallsyms_address_to_symbol(off
,
75 &mod_name
, &mod_start
, &mod_end
,
76 &sec_name
, &sec_start
, &sec_end
,
77 &sym_name
, &sym_start
, &sym_end
) == 0) {
81 value
= (db_expr_t
) sym_start
;
83 name
= (char *) sym_name
;
89 if (value
>= DB_SMALL_VALUE_MIN
&& value
<= DB_SMALL_VALUE_MAX
) {
94 if (name
== 0 || d
>= db_maxoff
) {
98 /* 2) backup to start of function (SOF)
99 * 3) delineate instruction boundaries, find instruction length too.
103 printk("function %s", sym_name
);
106 /* 4) skip instructions until we get to our faulty address */
108 while(cur_value
< sec_end
) {
111 // db_printsym(cur_value, DB_STGY_PROC);
115 prev_value
=cur_value
;
119 //cur_value=db_disasm(prev_value, FALSE);
122 cur_value
=my_disasm(prev_value
, FALSE
);
125 /* 4a) bail out if instruction is leave (0xc9) */
126 if(cur_value
-prev_value
== 1) {
128 c
=(unsigned char *) prev_value
;
129 if(text_read_ub(c
)==0xc9) {
130 if(verbose
) printk("bailing out as we hit a leave\n");
135 /* 5a) init fault: from SOF, look for movl $X, -Y(%ebp),
136 * (C645Fxxx or C745Fxxx) and replace with nop.
138 if(type
==INIT_FAULT
) {
140 c
=(unsigned char *) prev_value
;
142 if(*c
==0x66 || *c
==0x67)
143 c
++; /* override prefix */
145 if(*c
==0xC6 || *c
==0xC7)
146 c
++; /* movb or movl imm */
156 found
=1; /* negative displacement */
162 } else if(type
==NOP_FAULT
|| type
==STOP_FAULT
) {
163 /* 5b) nop*: replace instruction with nop */
168 } else if(type
==DST_FAULT
|| type
==SRC_FAULT
) {
169 /* 5c) dst/src: flip bits in mod/rm, sib, disp or imm fields */
170 if(cur_value
>off
&& (cur_value
-prev_value
) > 1) {
174 } else if(type
==BRANCH_FAULT
|| type
==LOOP_FAULT
) {
175 /* 5e) brc*: search forward utnil we hit a Jxx or rep (F3 or F2).
176 * replace instr with nop.
180 c
=(unsigned char *) prev_value
;
182 /* look for repX prefix */
184 if(text_read_ub(c
)==0xf3 || text_read_ub(c
)==0xf2) {
186 printk("found repX prefix\n");
187 /* take out repX prefix only */
189 cur_value
=prev_value
+1;
191 } else if( (text_read_ub(c
)&0xf0)==0x70 ||
192 (text_read_ub(c
)>=0xe0 && text_read_ub(c
)<=0xe2) ) {
193 /* look for jXX 8 (7X), loop,jcx (e0-3), jXX 16/32 (0f 8X) */
196 printk("found jXX rel8, loop or jcx\n");
198 } else if(text_read_ub(c
)==0x66 ||
199 text_read_ub(c
)==0x67) { /* override prefix */
201 } else if(text_read_ub(c
++)==0xf && (text_read_ub(c
)&0xf0)==0x80 ) {
202 found
=1; /* 0x0f 0x8X */
203 if(verbose
) printk("found branch!\n");
206 } else if(type
==PTR_FAULT
) {
207 /* 5f) ptr: if instruction has regmodrm byte (i_has_modrm),
208 * and mod field has address ([eyy]dispxx), eyy!=ebp
209 * flip 1 bit in lower byte (0x0f) or any bit in following
210 * bytes (sib, imm or disp).
212 if(cur_value
>off
&& modAddr
) {
214 c
=(unsigned char *) modAddr
;
215 if( text_read_ub(c
)>0x3f && text_read_ub(c
)<0xc0 &&
216 (text_read_ub(c
)&7)!=5 ) {
221 } else if(type
==INTERFACE_FAULT
) {
222 /* 5f) i/f: look for movl XX(ebp), reg or movb XX(ebp), reg,
223 * where XX is positive. replace instr with nop.
224 * movl=0x8a, movb=0x8b, mod=01XXX101 (disp8[ebp]), disp>0
227 c
=(unsigned char *) prev_value
;
228 if( text_read_ub(c
)==0x8a || text_read_ub(c
)==0x8b) {
230 if( ((text_read_ub(c
++))&0xc7)==0x45 && (text_read_ub(c
)&0x80)==0 ) {
231 /* 75% chance that we'll choose the next arg */
236 if(verbose
) printk("skipped...\n");
240 }else if(type
==IRQ_FAULT
) {
241 /* 5g) i/f: look for push reg or offset(reg) / popf,
242 * where XX is positive. replace instr with nop.
243 * movl=0x8a, movb=0x8b, mod=01XXX101 (disp8[ebp]), disp>0
246 c
=(unsigned char *) prev_value
;
247 if (((text_read_ub(c
) & 0xf8) == 0x50) ||
248 (text_read_ub(c
) == 0xff)) {
249 if (text_read_ub(c
) == 0xff) {
253 // Look for push x(ebp)
255 if ((text_read_ub(c
) & 0x78) != 0x70) {
264 if (text_read_ub(c
) == 0x9d) {
266 // Increment cur_value to include the
277 /* if we're doing nop fault, then we're done.
280 *instr_len
=cur_value
-prev_value
;
284 if (d
) printk("+0x%x", d
);
285 printk(" @ %x, ", value
);
286 printk("instr @ %x, len=%d, ", off
, *instr_len
);
290 if(verbose
) printk("cannot locate instruction in function\n");