xref: /csrg-svn/old/adb/adb.vax/access.c (revision 17254)
114467Ssam #ifndef lint
2*17254Ssam static	char sccsid[] = "@(#)access.c	4.8 10/13/84";
314467Ssam #endif
43753Sroot /*
53778Sroot  * Adb: access data in file/process address space.
63753Sroot  *
73778Sroot  * The routines in this file access referenced data using
83778Sroot  * the maps to access files, ptrace to access subprocesses,
93778Sroot  * or the system page tables when debugging the kernel,
103778Sroot  * to translate virtual to physical addresses.
113753Sroot  */
123753Sroot 
133753Sroot #include "defs.h"
143753Sroot 
153778Sroot 
163753Sroot MAP		txtmap;
173753Sroot MAP		datmap;
183753Sroot INT		wtflag;
193753Sroot STRING		errflg;
203753Sroot INT		errno;
213753Sroot 
223753Sroot INT		pid;
233753Sroot 
243778Sroot /*
253778Sroot  * Primitives: put a value in a space, get a value from a space
263778Sroot  * and get a word or byte not returning if an error occurred.
273778Sroot  */
put(addr,space,value)283778Sroot put(addr, space, value)
293778Sroot     off_t addr; { (void) access(WT, addr, space, value); }
303753Sroot 
313778Sroot u_int
get(addr,space)323778Sroot get(addr, space)
333778Sroot     off_t addr; { return (access(RD, addr, space, 0)); };
343753Sroot 
353778Sroot u_int
chkget(addr,space)363778Sroot chkget(addr, space)
373778Sroot     off_t addr; { u_int w = get(addr, space); chkerr(); return(w); }
383753Sroot 
393778Sroot u_int
bchkget(addr,space)403778Sroot bchkget(addr, space)
413778Sroot     off_t addr; { return(chkget(addr, space) & LOBYTE); }
423778Sroot 
433778Sroot /*
443778Sroot  * Read/write according to mode at address addr in i/d space.
453778Sroot  * Value is quantity to be written, if write.
463778Sroot  *
473778Sroot  * This routine decides whether to get the data from the subprocess
483778Sroot  * address space with ptrace, or to get it from the files being
493778Sroot  * debugged.
503778Sroot  *
513778Sroot  * When the kernel is being debugged with the -k flag we interpret
523778Sroot  * the system page tables for data space, mapping p0 and p1 addresses
533778Sroot  * relative to the ``current'' process (as specified by its p_addr in
543778Sroot  * <p) and mapping system space addresses through the system page tables.
553778Sroot  */
access(mode,addr,space,value)563778Sroot access(mode, addr, space, value)
573778Sroot 	int mode, space, value;
583778Sroot 	off_t addr;
593753Sroot {
603778Sroot 	int rd = mode == RD;
613778Sroot 	int file, w;
623753Sroot 
633778Sroot 	if (space == NSP)
643778Sroot 		return(0);
653778Sroot 	if (pid) {
66*17254Ssam 		int pmode = (space&DSP ?
67*17254Ssam 		    (rd ? PT_READ_D : PT_WRITE_D) :
68*17254Ssam 		    (rd ? PT_READ_I : PT_WRITE_I));
693753Sroot 
703778Sroot 		w = ptrace(pmode, pid, addr, value);
713778Sroot 		if (errno)
723778Sroot 			rwerr(space);
733778Sroot 		return (w);
743778Sroot 	}
753778Sroot 	w = 0;
763778Sroot 	if (mode==WT && wtflag==0)
773778Sroot 		error("not in write mode");
783780Sroot 	if (!chkmap(&addr, space))
793778Sroot 		return (0);
803778Sroot 	file = (space&DSP) ? datmap.ufd : txtmap.ufd;
813778Sroot 	if (kernel && space == DSP) {
823778Sroot 		addr = vtophys(addr);
833778Sroot 		if (addr < 0)
843778Sroot 			return (0);
853778Sroot 	}
867322Swnj 	if (physrw(file, addr, rd ? &w : &value, rd) < 0)
873778Sroot 		rwerr(space);
883778Sroot 	return (w);
893753Sroot }
903753Sroot 
913778Sroot /*
923778Sroot  * When looking at kernel data space through /dev/mem or
933778Sroot  * with a core file, do virtual memory mapping.
943778Sroot  */
vtophys(addr)953778Sroot vtophys(addr)
963778Sroot 	off_t addr;
973753Sroot {
983778Sroot 	int oldaddr = addr;
993778Sroot 	int v;
1003778Sroot 	struct pte pte;
1013753Sroot 
1023778Sroot 	addr &= ~0xc0000000;
1033778Sroot 	v = btop(addr);
1043778Sroot 	switch (oldaddr&0xc0000000) {
1053753Sroot 
1063778Sroot 	case 0xc0000000:
1073778Sroot 	case 0x80000000:
1083778Sroot 		/*
1093778Sroot 		 * In system space get system pte.  If
1103778Sroot 		 * valid or reclaimable then physical address
1113778Sroot 		 * is combination of its page number and the page
1123778Sroot 		 * offset of the original address.
1133778Sroot 		 */
1143778Sroot 		if (v >= slr)
1153778Sroot 			goto oor;
1163778Sroot 		addr = ((long)(sbr+v)) &~ 0x80000000;
1173778Sroot 		goto simple;
1183778Sroot 
1193778Sroot 	case 0x40000000:
1203778Sroot 		/*
1213778Sroot 		 * In p1 space must not be in shadow region.
1223778Sroot 		 */
1233778Sroot 		if (v < pcb.pcb_p1lr)
1243778Sroot 			goto oor;
1253778Sroot 		addr = pcb.pcb_p1br+v;
1263778Sroot 		break;
1273778Sroot 
1283778Sroot 	case 0x00000000:
1293778Sroot 		/*
1303778Sroot 		 * In p0 space must not be off end of region.
1313778Sroot 		 */
1323778Sroot 		if (v >= pcb.pcb_p0lr)
1333778Sroot 			goto oor;
1343778Sroot 		addr = pcb.pcb_p0br+v;
1353778Sroot 		break;
1363778Sroot 	oor:
1373778Sroot 		errflg = "address out of segment";
1383778Sroot 		return (-1);
1393778Sroot 	}
1403778Sroot 	/*
1413778Sroot 	 * For p0/p1 address, user-level page table should
1423778Sroot 	 * be in kernel vm.  Do second-level indirect by recursing.
1433778Sroot 	 */
1443778Sroot 	if ((addr & 0x80000000) == 0) {
1453778Sroot 		errflg = "bad p0br or p1br in pcb";
1463778Sroot 		return (-1);
1473778Sroot 	}
1483778Sroot 	addr = vtophys(addr);
1493778Sroot simple:
1503778Sroot 	/*
1513778Sroot 	 * Addr is now address of the pte of the page we
1523778Sroot 	 * are interested in; get the pte and paste up the
1533778Sroot 	 * physical address.
1543778Sroot 	 */
1553778Sroot 	if (physrw(fcor, addr, (int *)&pte, 1) < 0) {
1563778Sroot 		errflg = "page table botch";
1573778Sroot 		return (-1);
1583778Sroot 	}
1593778Sroot 	/* SHOULD CHECK NOT I/O ADDRESS; NEED CPU TYPE! */
1603778Sroot 	if (pte.pg_v == 0 && (pte.pg_fod || pte.pg_pfnum == 0)) {
1613778Sroot 		errflg = "page not valid/reclaimable";
1623778Sroot 		return (-1);
1633778Sroot 	}
1643778Sroot 	return (ptob(pte.pg_pfnum) + (oldaddr & PGOFSET));
1653753Sroot }
1663753Sroot 
rwerr(space)1673778Sroot rwerr(space)
1683778Sroot 	int space;
1693777Sroot {
1703777Sroot 
1713778Sroot 	if (space & DSP)
1723778Sroot 		errflg = "data address not found";
1733778Sroot 	else
1743778Sroot 		errflg = "text address not found";
1753777Sroot }
1763777Sroot 
physrw(file,addr,aw,rd)1773778Sroot physrw(file, addr, aw, rd)
1783778Sroot 	off_t addr;
1793778Sroot 	int *aw, rd;
1803777Sroot {
1813777Sroot 
1823778Sroot 	if (longseek(file,addr)==0 ||
1833778Sroot 	    (rd ? read(file,aw,sizeof(int)) : write(file,aw,sizeof(int))) < 1)
1843778Sroot 		return (-1);
1853778Sroot 	return (0);
1863777Sroot }
1873778Sroot 
chkmap(addr,space)1883778Sroot chkmap(addr,space)
1893778Sroot 	REG L_INT	*addr;
1903753Sroot 	REG INT		space;
1913753Sroot {
1923753Sroot 	REG MAPPTR amap;
1933753Sroot 	amap=((space&DSP?&datmap:&txtmap));
1943778Sroot 	IF space&STAR ORF !within(*addr,amap->b1,amap->e1)
1953778Sroot 	THEN IF within(*addr,amap->b2,amap->e2)
1963778Sroot 	     THEN *addr += (amap->f2)-(amap->b2);
1973778Sroot 	     ELSE rwerr(space); return(0);
1983753Sroot 	     FI
1993778Sroot 	ELSE *addr += (amap->f1)-(amap->b1);
2003753Sroot 	FI
2013753Sroot 	return(1);
2023753Sroot }
2033753Sroot 
within(addr,lbd,ubd)2043778Sroot within(addr,lbd,ubd)
2053778Sroot     u_int addr, lbd, ubd; { return(addr>=lbd && addr<ubd); }
2063753Sroot 
longseek(f,a)2073753Sroot longseek(f, a)
2083778Sroot     off_t a; { return(lseek(f, a, 0) != -1); }
209