13753Sroot /* 2*3778Sroot * Adb: access data in file/process address space. 33753Sroot * 4*3778Sroot * The routines in this file access referenced data using 5*3778Sroot * the maps to access files, ptrace to access subprocesses, 6*3778Sroot * or the system page tables when debugging the kernel, 7*3778Sroot * to translate virtual to physical addresses. 83753Sroot */ 9*3778Sroot #define dprintf if (var[varchk('d')]) printf 103753Sroot 113753Sroot #include "defs.h" 12*3778Sroot static char sccsid[] = "@(#)access.c 4.3 05/15/81"; 133753Sroot 14*3778Sroot 153753Sroot MAP txtmap; 163753Sroot MAP datmap; 173753Sroot INT wtflag; 183753Sroot STRING errflg; 193753Sroot INT errno; 203753Sroot 213753Sroot INT pid; 223753Sroot 23*3778Sroot /* 24*3778Sroot * Primitives: put a value in a space, get a value from a space 25*3778Sroot * and get a word or byte not returning if an error occurred. 26*3778Sroot */ 27*3778Sroot put(addr, space, value) 28*3778Sroot off_t addr; { (void) access(WT, addr, space, value); } 293753Sroot 30*3778Sroot u_int 31*3778Sroot get(addr, space) 32*3778Sroot off_t addr; { return (access(RD, addr, space, 0)); }; 333753Sroot 34*3778Sroot u_int 35*3778Sroot chkget(addr, space) 36*3778Sroot off_t addr; { u_int w = get(addr, space); chkerr(); return(w); } 373753Sroot 38*3778Sroot u_int 39*3778Sroot bchkget(addr, space) 40*3778Sroot off_t addr; { return(chkget(addr, space) & LOBYTE); } 41*3778Sroot 42*3778Sroot /* 43*3778Sroot * Read/write according to mode at address addr in i/d space. 44*3778Sroot * Value is quantity to be written, if write. 45*3778Sroot * 46*3778Sroot * This routine decides whether to get the data from the subprocess 47*3778Sroot * address space with ptrace, or to get it from the files being 48*3778Sroot * debugged. 49*3778Sroot * 50*3778Sroot * When the kernel is being debugged with the -k flag we interpret 51*3778Sroot * the system page tables for data space, mapping p0 and p1 addresses 52*3778Sroot * relative to the ``current'' process (as specified by its p_addr in 53*3778Sroot * <p) and mapping system space addresses through the system page tables. 54*3778Sroot */ 55*3778Sroot access(mode, addr, space, value) 56*3778Sroot int mode, space, value; 57*3778Sroot off_t addr; 583753Sroot { 59*3778Sroot int rd = mode == RD; 60*3778Sroot int file, w; 613753Sroot 62*3778Sroot dprintf("access(%X)\n", addr); 63*3778Sroot if (space == NSP) 64*3778Sroot return(0); 65*3778Sroot if (pid) { 66*3778Sroot int pmode = (space&DSP?(rd?RDUSER:WDUSER):(rd?RIUSER:WIUSER)); 673753Sroot 68*3778Sroot w = ptrace(pmode, pid, addr, value); 69*3778Sroot if (errno) 70*3778Sroot rwerr(space); 71*3778Sroot return (w); 72*3778Sroot } 73*3778Sroot w = 0; 74*3778Sroot if (mode==WT && wtflag==0) 75*3778Sroot error("not in write mode"); 76*3778Sroot if (!chkmap(&addr, space)) { 77*3778Sroot dprintf("chkmap failed\n"); 78*3778Sroot return (0); 79*3778Sroot } 80*3778Sroot file = (space&DSP) ? datmap.ufd : txtmap.ufd; 81*3778Sroot if (kernel && space == DSP) { 82*3778Sroot dprintf("calling vtophys(%X)... ", addr); 83*3778Sroot addr = vtophys(addr); 84*3778Sroot if (addr < 0) 85*3778Sroot return (0); 86*3778Sroot dprintf("got %X\n", addr); 87*3778Sroot } 88*3778Sroot if (physrw(file, addr, &w, rd) < 0) 89*3778Sroot rwerr(space); 90*3778Sroot return (w); 913753Sroot } 923753Sroot 93*3778Sroot /* 94*3778Sroot * When looking at kernel data space through /dev/mem or 95*3778Sroot * with a core file, do virtual memory mapping. 96*3778Sroot */ 97*3778Sroot vtophys(addr) 98*3778Sroot off_t addr; 993753Sroot { 100*3778Sroot int oldaddr = addr; 101*3778Sroot int v; 102*3778Sroot struct pte pte; 1033753Sroot 104*3778Sroot addr &= ~0xc0000000; 105*3778Sroot v = btop(addr); 106*3778Sroot dprintf("addr %X v %X\n", addr, v); 107*3778Sroot switch (oldaddr&0xc0000000) { 1083753Sroot 109*3778Sroot case 0xc0000000: 110*3778Sroot case 0x80000000: 111*3778Sroot /* 112*3778Sroot * In system space get system pte. If 113*3778Sroot * valid or reclaimable then physical address 114*3778Sroot * is combination of its page number and the page 115*3778Sroot * offset of the original address. 116*3778Sroot */ 117*3778Sroot if (v >= slr) 118*3778Sroot goto oor; 119*3778Sroot addr = ((long)(sbr+v)) &~ 0x80000000; 120*3778Sroot goto simple; 121*3778Sroot 122*3778Sroot case 0x40000000: 123*3778Sroot /* 124*3778Sroot * In p1 space must not be in shadow region. 125*3778Sroot */ 126*3778Sroot if (v < pcb.pcb_p1lr) 127*3778Sroot goto oor; 128*3778Sroot addr = pcb.pcb_p1br+v; 129*3778Sroot break; 130*3778Sroot 131*3778Sroot case 0x00000000: 132*3778Sroot /* 133*3778Sroot * In p0 space must not be off end of region. 134*3778Sroot */ 135*3778Sroot if (v >= pcb.pcb_p0lr) 136*3778Sroot goto oor; 137*3778Sroot addr = pcb.pcb_p0br+v; 138*3778Sroot break; 139*3778Sroot oor: 140*3778Sroot dprintf("out of range\n"); 141*3778Sroot errflg = "address out of segment"; 142*3778Sroot return (-1); 143*3778Sroot } 144*3778Sroot /* 145*3778Sroot * For p0/p1 address, user-level page table should 146*3778Sroot * be in kernel vm. Do second-level indirect by recursing. 147*3778Sroot */ 148*3778Sroot if ((addr & 0x80000000) == 0) { 149*3778Sroot errflg = "bad p0br or p1br in pcb"; 150*3778Sroot dprintf("bad p0/p1br\n"); 151*3778Sroot return (-1); 152*3778Sroot } 153*3778Sroot dprintf("calling vtophys recursively(%X)\n", addr); 154*3778Sroot addr = vtophys(addr); 155*3778Sroot dprintf("result %X\n", addr); 156*3778Sroot simple: 157*3778Sroot /* 158*3778Sroot * Addr is now address of the pte of the page we 159*3778Sroot * are interested in; get the pte and paste up the 160*3778Sroot * physical address. 161*3778Sroot */ 162*3778Sroot if (physrw(fcor, addr, (int *)&pte, 1) < 0) { 163*3778Sroot errflg = "page table botch"; 164*3778Sroot return (-1); 165*3778Sroot } 166*3778Sroot /* SHOULD CHECK NOT I/O ADDRESS; NEED CPU TYPE! */ 167*3778Sroot if (pte.pg_v == 0 && (pte.pg_fod || pte.pg_pfnum == 0)) { 168*3778Sroot errflg = "page not valid/reclaimable"; 169*3778Sroot return (-1); 170*3778Sroot } 171*3778Sroot return (ptob(pte.pg_pfnum) + (oldaddr & PGOFSET)); 1723753Sroot } 1733753Sroot 174*3778Sroot rwerr(space) 175*3778Sroot int space; 1763777Sroot { 1773777Sroot 178*3778Sroot if (space & DSP) 179*3778Sroot errflg = "data address not found"; 180*3778Sroot else 181*3778Sroot errflg = "text address not found"; 1823777Sroot } 1833777Sroot 184*3778Sroot physrw(file, addr, aw, rd) 185*3778Sroot off_t addr; 186*3778Sroot int *aw, rd; 1873777Sroot { 1883777Sroot 189*3778Sroot dprintf("physrw(%X)... ", addr); 190*3778Sroot if (longseek(file,addr)==0 || 191*3778Sroot (rd ? read(file,aw,sizeof(int)) : write(file,aw,sizeof(int))) < 1) 192*3778Sroot return (-1); 193*3778Sroot dprintf("got %X\n", *aw); 194*3778Sroot return (0); 1953777Sroot } 196*3778Sroot 197*3778Sroot chkmap(addr,space) 198*3778Sroot REG L_INT *addr; 1993753Sroot REG INT space; 2003753Sroot { 2013753Sroot REG MAPPTR amap; 2023753Sroot amap=((space&DSP?&datmap:&txtmap)); 203*3778Sroot IF space&STAR ORF !within(*addr,amap->b1,amap->e1) 204*3778Sroot THEN IF within(*addr,amap->b2,amap->e2) 205*3778Sroot THEN *addr += (amap->f2)-(amap->b2); 206*3778Sroot ELSE rwerr(space); return(0); 2073753Sroot FI 208*3778Sroot ELSE *addr += (amap->f1)-(amap->b1); 2093753Sroot FI 2103753Sroot return(1); 2113753Sroot } 2123753Sroot 213*3778Sroot within(addr,lbd,ubd) 214*3778Sroot u_int addr, lbd, ubd; { return(addr>=lbd && addr<ubd); } 2153753Sroot 2163753Sroot longseek(f, a) 217*3778Sroot off_t a; { return(lseek(f, a, 0) != -1); } 218