re-enable munmap().
[minix.git] / commands / swifi / extra.c
blobb8233f69e4569b5a401e28dc7eb2517797530696
1 /*
2 Declaration for Linux kernel compatibility
3 */
5 #include <assert.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/ptrace.h>
12 #include "extra.h"
14 pid_t victim_pid= -1;
15 char *victim_exe= NULL;
17 #define TRAP_BIT (0x80000000)
19 static struct nlist *exe_nlist;
20 static int exe_nlist_n;
22 /* unsigned long __get_free_page(int type) { assert(0); } */
23 /* void *kmalloc(size_t size, int type) { assert(0); } */
24 void free_page(unsigned long page) { assert(0); }
25 /* void kfree(void *mem) { assert(0); } */
26 void vfree(void *mem) { assert(0); }
28 size_t strncpy_from_user(char *addr, const char *user_name, size_t size)
29 { assert(0); }
31 /* void lock_kernel(void) { assert(0); } */
32 /* void unlock_kernel(void) { assert(0); } */
33 void __asm__(char *str) { assert(0); }
35 extern void *__vmalloc(unsigned long size, int gfp_mask, pgprot_t prot)
36 { assert(0); }
38 #if 0
39 void kallsyms_sections(void *infop,
40 int (*fp)(void *token, const char *modname, const char *secname,
41 ElfW(Addr) secstart, ElfW(Addr) secend, ElfW(Word) secflags))
42 { assert(0); }
43 #endif
45 unsigned long __generic_copy_to_user(void *x, const void *y, unsigned long z)
46 { assert(0); }
47 unsigned long __generic_copy_from_user(void *x, const void *y, unsigned long z)
48 { assert(0); }
50 /* void read_lock(struct lock *lock) { assert(0); } */
51 /* void read_unlock(struct lock *lock) { assert(0); } */
52 void udelay(unsigned long usecs) { assert(0); }
53 int copy_to_user(void * result_record, void *res, size_t size)
55 memcpy(result_record, res, size);
56 return 0;
59 void panic(char *str) { assert(0); }
61 void printk(char *fmt, ...)
63 va_list ap;
65 va_start(ap, fmt);
66 vfprintf(stderr, fmt, ap);
67 va_end(ap);
70 int kallsyms_address_to_symbol(db_expr_t off,
71 const char * *mod_name, unsigned long *mod_start, unsigned long *mod_end,
72 const char * *sec_name, unsigned long *sec_start, unsigned long *sec_end,
73 const char * *sym_name, unsigned long *sym_start, unsigned long *sym_end)
75 static char name[sizeof(((struct nlist *)0)->n_name)+1];
77 int i;
78 unsigned long btext, etext;
79 struct nlist *below, *above;
81 off &= ~TRAP_BIT;
82 load_nlist(victim_exe, &btext, &etext);
83 below= above= NULL;
84 for (i= 0; i<exe_nlist_n; i++)
86 if ((exe_nlist[i].n_sclass & N_SECT) != N_TEXT)
87 continue;
88 if (exe_nlist[i].n_value <= off)
90 if (!below || exe_nlist[i].n_value > below->n_value)
91 below= &exe_nlist[i];
93 if (exe_nlist[i].n_value > off)
95 if (!above || exe_nlist[i].n_value < above->n_value)
96 above= &exe_nlist[i];
99 #if 0
100 if (below)
102 printf("found '%.*s' at 0x%x\n", sizeof(below->n_name),
103 below->n_name, below->n_value);
105 if (above)
107 printf("found '%.*s' at 0x%x\n", sizeof(above->n_name),
108 above->n_name, above->n_value);
110 #endif
112 btext |= TRAP_BIT;
113 etext |= TRAP_BIT;
115 *mod_name = victim_exe;
116 *mod_start = btext;
117 *mod_end = etext;
118 *sec_name = ".text";
119 *sec_start = btext;
120 *sec_end = etext;
122 assert(below && above);
124 memcpy(name, below->n_name, sizeof(below->n_name));
125 name[sizeof(below->n_name)]= '\0';
126 *sym_name= name;
128 *sym_start= below->n_value | TRAP_BIT;
129 *sym_end= above->n_value | TRAP_BIT;
131 return 1;
134 struct module *module_list;
135 struct task_struct *task_list;
136 struct lock tasklist_lock;
138 unsigned long text_read_ul(void *addr)
140 int i;
141 unsigned long value;
143 for (i= 0; i<sizeof(value); i++)
145 ((unsigned char *)&value)[i]= text_read_ub((char *)addr+i);
147 return value;
150 unsigned char text_read_ub(void *addr)
152 int v;
153 unsigned long vaddr;
155 vaddr= (unsigned long)addr;
156 vaddr &= ~TRAP_BIT;
157 v= ptrace(T_READB_INS, victim_pid, vaddr, 0);
158 if (v < 0)
160 fprintf(stderr,
161 "text_read_ub: trace T_READB_INS failed on pid %d, addr 0x%x: %s\n",
162 victim_pid, vaddr, strerror(errno));
163 exit(1);
165 return v;
168 void text_write_ul(void *addr, unsigned long value)
170 int i;
172 for (i= 0; i<sizeof(value); i++)
174 text_write_ub((char *)addr+i, ((unsigned char *)&value)[i]);
178 void text_write_ub(void *addr, unsigned char value)
180 int v;
181 unsigned long vaddr;
183 vaddr= (unsigned long)addr;
184 vaddr &= ~TRAP_BIT;
185 v= ptrace(T_WRITEB_INS, victim_pid, vaddr, value);
186 if (v < 0)
188 fprintf(stderr,
189 "text_read_ub: trace T_WRITEB_INS failed on pid %d, addr 0x%x: %s\n",
190 victim_pid, vaddr, strerror(errno));
191 exit(1);
195 void load_nlist(exe_name, btextp, etextp)
196 char *exe_name;
197 unsigned long *btextp;
198 unsigned long *etextp;
200 int i;
201 unsigned long btext, etext;
203 if (!exe_nlist)
205 exe_nlist_n= read_nlist(exe_name, &exe_nlist);
206 if (exe_nlist_n <= 0)
208 if (exe_nlist_n == -1)
210 fprintf(stderr,
211 "error reading name list from '%s': %s\n",
212 exe_name, strerror(errno));
214 else
215 fprintf(stderr, "no name list in '%s'\n",
216 exe_name);
217 exit(1);
221 if (!btextp && !etextp)
222 return;
224 etext= 0;
225 btext= (unsigned long)-1;
226 for (i= 0; i<exe_nlist_n; i++)
228 if ((exe_nlist[i].n_sclass & N_SECT) != N_TEXT)
229 continue;
230 if (exe_nlist[i].n_value < btext)
231 btext= exe_nlist[i].n_value;
232 if (exe_nlist[i].n_value > etext)
233 etext= exe_nlist[i].n_value;
236 if (btext >= etext)
238 fprintf(stderr, "Bad btext (0x%x) or etext (0x%x) in %d\n",
239 btext, etext, exe_name);
240 exit(1);
243 btext |= TRAP_BIT;
244 etext |= TRAP_BIT;
246 if (btextp)
247 *btextp= btext;
248 if (etextp)
249 *etextp= etext;