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