xref: /csrg-svn/old/adb/adb.tahoe/access.c (revision 26416)
1*26416Ssam #ifndef lint
2*26416Ssam static	char sccsid[] = "@(#)access.c	1.1 (Berkeley) 02/25/86";
3*26416Ssam #endif
4*26416Ssam /*
5*26416Ssam  * Adb: access data in file/process address space.
6*26416Ssam  *
7*26416Ssam  * The routines in this file access referenced data using
8*26416Ssam  * the maps to access files, ptrace to access subprocesses,
9*26416Ssam  * or the system page tables when debugging the kernel,
10*26416Ssam  * to translate virtual to physical addresses.
11*26416Ssam  */
12*26416Ssam 
13*26416Ssam #include "defs.h"
14*26416Ssam 
15*26416Ssam MAP		txtmap;
16*26416Ssam MAP		datmap;
17*26416Ssam INT		wtflag;
18*26416Ssam STRING		errflg;
19*26416Ssam INT		errno;
20*26416Ssam 
21*26416Ssam L_INT		pid;
22*26416Ssam 
23*26416Ssam /*
24*26416Ssam  * Primitives: put a value in a space, get a value from a space
25*26416Ssam  * and get a word or byte not returning if an error occurred.
26*26416Ssam  */
put(addr,space,value)27*26416Ssam put(addr, space, value)
28*26416Ssam     off_t addr; { (void) access(WT, addr, space, value); }
29*26416Ssam 
30*26416Ssam u_int
get(addr,space)31*26416Ssam get(addr, space)
32*26416Ssam     off_t addr; { return (access(RD, addr, space, 0)); };
33*26416Ssam 
34*26416Ssam u_int
chkget(addr,space)35*26416Ssam chkget(addr, space)
36*26416Ssam     off_t addr; { u_int w = get(addr, space); chkerr(); return(w); }
37*26416Ssam 
38*26416Ssam u_int
bchkget(addr,space)39*26416Ssam bchkget(addr, space)
40*26416Ssam     off_t addr; { return (byte(chkget(addr, space))); }
41*26416Ssam 
42*26416Ssam /*
43*26416Ssam  * Read/write according to mode at address addr in i/d space.
44*26416Ssam  * Value is quantity to be written, if write.
45*26416Ssam  *
46*26416Ssam  * This routine decides whether to get the data from the subprocess
47*26416Ssam  * address space with ptrace, or to get it from the files being
48*26416Ssam  * debugged.
49*26416Ssam  *
50*26416Ssam  * When the kernel is being debugged with the -k flag we interpret
51*26416Ssam  * the system page tables for data space, mapping p0 and p1 addresses
52*26416Ssam  * relative to the ``current'' process (as specified by its p_addr in
53*26416Ssam  * <p) and mapping system space addresses through the system page tables.
54*26416Ssam  */
access(mode,addr,space,value)55*26416Ssam access(mode, addr, space, value)
56*26416Ssam 	int mode, space, value;
57*26416Ssam 	off_t addr;
58*26416Ssam {
59*26416Ssam 	int rd = mode == RD;
60*26416Ssam 	int file, w;
61*26416Ssam 
62*26416Ssam 	if (space == NSP)
63*26416Ssam 		return(0);
64*26416Ssam 	if (pid) {
65*26416Ssam 		int pmode = (space&DSP?(rd?RDUSER:WDUSER):(rd?RIUSER:WIUSER));
66*26416Ssam 
67*26416Ssam 		w = ptrace(pmode, pid, addr, value);
68*26416Ssam 		if (errno)
69*26416Ssam 			rwerr(space);
70*26416Ssam 		return (w);
71*26416Ssam 	}
72*26416Ssam 	w = 0;
73*26416Ssam 	if (mode==WT && wtflag==0)
74*26416Ssam 		error("not in write mode");
75*26416Ssam 	if (!chkmap(&addr, space))
76*26416Ssam 		return (0);
77*26416Ssam 	file = (space&DSP) ? datmap.ufd : txtmap.ufd;
78*26416Ssam 	if (kernel && space == DSP) {
79*26416Ssam 		addr = vtophys(addr);
80*26416Ssam 		if (addr < 0)
81*26416Ssam 			return (0);
82*26416Ssam 	}
83*26416Ssam 	if (physrw(file, addr, rd ? &w : &value, rd) < 0)
84*26416Ssam 		rwerr(space);
85*26416Ssam 	return (w);
86*26416Ssam }
87*26416Ssam 
88*26416Ssam /*
89*26416Ssam  * When looking at kernel data space through /dev/mem or
90*26416Ssam  * with a core file, do virtual memory mapping.
91*26416Ssam  */
vtophys(addr)92*26416Ssam vtophys(addr)
93*26416Ssam 	off_t addr;
94*26416Ssam {
95*26416Ssam 	int oldaddr = addr;
96*26416Ssam 	int v;
97*26416Ssam 	struct pte pte;
98*26416Ssam 
99*26416Ssam 	addr &= ~0xc0000000;
100*26416Ssam 	v = btop(addr);
101*26416Ssam 	switch (oldaddr&0xc0000000) {
102*26416Ssam 
103*26416Ssam 	case 0xc0000000:
104*26416Ssam 		/*
105*26416Ssam 		 * In system space get system pte.  If
106*26416Ssam 		 * valid or reclaimable then physical address
107*26416Ssam 		 * is combination of its page number and the page
108*26416Ssam 		 * offset of the original address.
109*26416Ssam 		 */
110*26416Ssam 		if (v >= slr)
111*26416Ssam 			goto oor;
112*26416Ssam 		addr = ((long)(sbr+v)) &~ 0xc0000000;
113*26416Ssam 		goto simple;
114*26416Ssam 
115*26416Ssam 	case 0x80000000:
116*26416Ssam 		/*
117*26416Ssam 		 * In p2 spce must not be in shadow region.
118*26416Ssam 		 */
119*26416Ssam 		if (v < pcb.pcb_p2lr)
120*26416Ssam 			goto oor;
121*26416Ssam 		addr = (long)(pcb.pcb_p2br+v);
122*26416Ssam 		break;
123*26416Ssam 
124*26416Ssam 	case 0x40000000:
125*26416Ssam 		/*
126*26416Ssam 		 * In p1 space everything is verboten (for now).
127*26416Ssam 		 */
128*26416Ssam 		goto oor;
129*26416Ssam 
130*26416Ssam 	case 0x00000000:
131*26416Ssam 		/*
132*26416Ssam 		 * In p0 space must not be off end of region.
133*26416Ssam 		 */
134*26416Ssam 		if (v >= pcb.pcb_p0lr)
135*26416Ssam 			goto oor;
136*26416Ssam 		addr = (long)(pcb.pcb_p0br+v);
137*26416Ssam 		break;
138*26416Ssam 	oor:
139*26416Ssam 		errflg = "address out of segment";
140*26416Ssam 		return (-1);
141*26416Ssam 	}
142*26416Ssam 	/*
143*26416Ssam 	 * For p0/p1/p2 address, user-level page table should
144*26416Ssam 	 * be in kernel vm.  Do second-level indirect by recursing.
145*26416Ssam 	 */
146*26416Ssam 	if ((addr & 0xc0000000) != 0xc0000000) {
147*26416Ssam 		errflg = "bad p0br, p1br, or p2br in pcb";
148*26416Ssam 		return (-1);
149*26416Ssam 	}
150*26416Ssam 	addr = vtophys(addr);
151*26416Ssam simple:
152*26416Ssam 	/*
153*26416Ssam 	 * Addr is now address of the pte of the page we
154*26416Ssam 	 * are interested in; get the pte and paste up the
155*26416Ssam 	 * physical address.
156*26416Ssam 	 */
157*26416Ssam 	if (physrw(fcor, addr, (int *)&pte, 1) < 0) {
158*26416Ssam 		errflg = "page table botch";
159*26416Ssam 		return (-1);
160*26416Ssam 	}
161*26416Ssam 	/* SHOULD CHECK NOT I/O ADDRESS; NEED CPU TYPE! */
162*26416Ssam 	if (pte.pg_v == 0 && (pte.pg_fod || pte.pg_pfnum == 0)) {
163*26416Ssam 		errflg = "page not valid/reclaimable";
164*26416Ssam 		return (-1);
165*26416Ssam 	}
166*26416Ssam 	return ((long)(ptob(pte.pg_pfnum) + (oldaddr & PGOFSET)));
167*26416Ssam }
168*26416Ssam 
rwerr(space)169*26416Ssam rwerr(space)
170*26416Ssam 	int space;
171*26416Ssam {
172*26416Ssam 
173*26416Ssam 	if (space & DSP)
174*26416Ssam 		errflg = "data address not found";
175*26416Ssam 	else
176*26416Ssam 		errflg = "text address not found";
177*26416Ssam }
178*26416Ssam 
physrw(file,addr,aw,rd)179*26416Ssam physrw(file, addr, aw, rd)
180*26416Ssam 	off_t addr;
181*26416Ssam 	int *aw, rd;
182*26416Ssam {
183*26416Ssam 
184*26416Ssam 	if (longseek(file,addr)==0 ||
185*26416Ssam 	    (rd ? read(file,aw,sizeof(int)) : write(file,aw,sizeof(int))) < 1)
186*26416Ssam 		return (-1);
187*26416Ssam 	return (0);
188*26416Ssam }
189*26416Ssam 
chkmap(addr,space)190*26416Ssam chkmap(addr,space)
191*26416Ssam 	REG L_INT	*addr;
192*26416Ssam 	REG INT 	space;
193*26416Ssam {
194*26416Ssam 	REG MAPPTR amap;
195*26416Ssam 
196*26416Ssam 	amap=((space&DSP?&datmap:&txtmap));
197*26416Ssam 	IF space&STAR ORF !within(*addr,amap->b1,amap->e1)
198*26416Ssam 	THEN IF within(*addr,amap->b2,amap->e2)
199*26416Ssam 	     THEN *addr += (amap->f2)-(amap->b2);
200*26416Ssam 	     ELSE rwerr(space); return(0);
201*26416Ssam 	     FI
202*26416Ssam 	ELSE *addr += (amap->f1)-(amap->b1);
203*26416Ssam 	FI
204*26416Ssam 	return(1);
205*26416Ssam }
206*26416Ssam 
within(addr,lbd,ubd)207*26416Ssam within(addr,lbd,ubd)
208*26416Ssam     u_int addr, lbd, ubd; { return(addr>=lbd && addr<ubd); }
209*26416Ssam 
longseek(f,a)210*26416Ssam longseek(f, a)
211*26416Ssam     off_t a; { return(lseek(f, a, 0) != -1); }
212