kvm_sparc64.c revision 101653
1/*-
2 * Copyright (c) 1989, 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software developed by the Computer Systems
6 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7 * BG 91-66 and contributed to Berkeley.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by the University of
20 *	California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 *	from: FreeBSD: src/lib/libkvm/kvm_i386.c,v 1.15 2001/10/10 17:48:43
38 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/lib/libkvm/kvm_sparc64.c 101653 2002-08-10 22:14:16Z jake $");
42
43#if defined(LIBC_SCCS) && !defined(lint)
44#if 0
45static char sccsid[] = "@(#)kvm_hp300.c	8.1 (Berkeley) 6/4/93";
46#endif
47#endif /* LIBC_SCCS and not lint */
48
49/*
50 * sparc64 machine dependent routines for kvm.
51 */
52
53#include <sys/param.h>
54#include <sys/user.h>
55#include <sys/proc.h>
56#include <sys/stat.h>
57#include <stdlib.h>
58#include <unistd.h>
59#include <nlist.h>
60#include <kvm.h>
61
62#include <vm/vm.h>
63#include <vm/vm_param.h>
64
65#include <machine/tte.h>
66#include <machine/tsb.h>
67
68#include <limits.h>
69
70#include "kvm_private.h"
71
72#ifndef btop
73#define	btop(x)		(sparc64_btop(x))
74#define	ptob(x)		(sparc64_ptob(x))
75#endif
76
77struct vmstate {
78	vm_offset_t	vm_tsb;
79	vm_size_t	vm_tsb_mask;
80};
81
82void
83_kvm_freevtop(kvm_t *kd)
84{
85	if (kd->vmst != 0) {
86		free(kd->vmst);
87	}
88}
89
90int
91_kvm_initvtop(kvm_t *kd)
92{
93	struct nlist nlist[2];
94	struct vmstate *vm;
95	vm_offset_t pa;
96	vm_size_t mask;
97
98	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
99	if (vm == NULL) {
100		_kvm_err(kd, kd->program, "cannot allocate vm");
101		return (-1);
102	}
103	kd->vmst = vm;
104	vm->vm_tsb = 0;
105
106	nlist[0].n_name = "tsb_kernel_phys";
107	nlist[1].n_name = "tsb_kernel_mask";
108	nlist[2].n_name = 0;
109
110	if (kvm_nlist(kd, nlist) != 0) {
111		_kvm_err(kd, kd->program, "bad namelist");
112		return (-1);
113	}
114	if (kvm_read(kd, nlist[0].n_value, &pa, sizeof(pa)) != sizeof(pa) ||
115	    kvm_read(kd, nlist[1].n_value, &mask, sizeof(mask)) !=
116	    sizeof(mask)) {
117		_kvm_err(kd, kd->program, "cannot read tsb_kernel_phys");
118		return (-1);
119	}
120	vm->vm_tsb = pa;
121	vm->vm_tsb_mask = mask;
122	return (0);
123}
124
125int
126_kvm_kvatop(kvm_t *kd, u_long va, u_long *pa)
127{
128	struct vmstate *vm;
129	struct tte tte;
130	u_long offset;
131	u_long tte_pa;
132	u_long vpn;
133
134	vpn = btop(va);
135	offset = va & PAGE_MASK;
136	tte_pa = kd->vmst->vm_tsb +
137	    ((vpn & kd->vmst->vm_tsb_mask) << TTE_SHIFT);
138
139	/* XXX This has to be a physical address read, kvm_read is virtual */
140	if (lseek(kd->pmfd, tte_pa, 0) == -1) {
141		_kvm_syserr(kd, kd->program, "_kvm_vatop: lseek");
142		goto invalid;
143	}
144	if (read(kd->pmfd, &tte, sizeof(tte)) != sizeof(tte)) {
145		_kvm_syserr(kd, kd->program, "_kvm_vatop: read");
146		goto invalid;
147	}
148	if (!tte_match(&tte, va))
149		goto invalid;
150
151	*pa = TTE_GET_PA(&tte) + offset;
152	return (PAGE_SIZE - offset);
153
154invalid:
155	_kvm_err(kd, 0, "invalid address (%x)", va);
156	return (0);
157}
158