kvm_sparc64.c revision 88615
1195801Smav/*-
2195801Smav * Copyright (c) 1989, 1992, 1993
3195801Smav *	The Regents of the University of California.  All rights reserved.
4195801Smav *
5195801Smav * This code is derived from software developed by the Computer Systems
6195801Smav * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7195801Smav * BG 91-66 and contributed to Berkeley.
8195801Smav *
9195801Smav * Redistribution and use in source and binary forms, with or without
10195801Smav * modification, are permitted provided that the following conditions
11195801Smav * are met:
12195801Smav * 1. Redistributions of source code must retain the above copyright
13195801Smav *    notice, this list of conditions and the following disclaimer.
14195801Smav * 2. Redistributions in binary form must reproduce the above copyright
15195801Smav *    notice, this list of conditions and the following disclaimer in the
16195801Smav *    documentation and/or other materials provided with the distribution.
17195801Smav * 3. All advertising materials mentioning features or use of this software
18195801Smav *    must display the following acknowledgement:
19195801Smav *	This product includes software developed by the University of
20195801Smav *	California, Berkeley and its contributors.
21195801Smav * 4. Neither the name of the University nor the names of its contributors
22195801Smav *    may be used to endorse or promote products derived from this software
23195801Smav *    without specific prior written permission.
24195801Smav *
25195801Smav * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26195801Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27195801Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28195801Smav * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29195801Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30195801Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31195801Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32195801Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33195801Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34195801Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35195801Smav * SUCH DAMAGE.
36195801Smav *
37195801Smav *	from: FreeBSD: src/lib/libkvm/kvm_i386.c,v 1.15 2001/10/10 17:48:43
38195801Smav */
39195801Smav
40195801Smav#include <sys/cdefs.h>
41195801Smav__FBSDID("$FreeBSD: head/lib/libkvm/kvm_sparc64.c 88615 2001-12-29 06:43:36Z jake $");
42195801Smav
43195801Smav#if defined(LIBC_SCCS) && !defined(lint)
44195801Smav#if 0
45195801Smavstatic char sccsid[] = "@(#)kvm_hp300.c	8.1 (Berkeley) 6/4/93";
46195801Smav#endif
47195801Smav#endif /* LIBC_SCCS and not lint */
48195801Smav
49195801Smav/*
50195801Smav * sparc64 machine dependent routines for kvm.
51195801Smav */
52195801Smav
53195801Smav#include <sys/param.h>
54195801Smav#include <sys/user.h>
55195801Smav#include <sys/proc.h>
56195801Smav#include <sys/stat.h>
57195801Smav#include <stdlib.h>
58195801Smav#include <unistd.h>
59195801Smav#include <nlist.h>
60195801Smav#include <kvm.h>
61195801Smav
62195801Smav#include <vm/vm.h>
63195801Smav#include <vm/vm_param.h>
64195801Smav
65195801Smav#include <machine/tte.h>
66195801Smav#include <machine/tsb.h>
67195801Smav
68195801Smav#include <limits.h>
69195801Smav
70195801Smav#include "kvm_private.h"
71195801Smav
72195801Smav#ifndef btop
73195801Smav#define	btop(x)		(sparc64_btop(x))
74195801Smav#define	ptob(x)		(sparc64_ptob(x))
75195801Smav#endif
76195801Smav
77195801Smavstruct vmstate {
78195801Smav	vm_offset_t	vm_tsb;
79195801Smav};
80195801Smav
81195801Smavvoid
82195801Smav_kvm_freevtop(kvm_t *kd)
83195801Smav{
84195801Smav	if (kd->vmst != 0) {
85195801Smav		free(kd->vmst);
86195801Smav	}
87195801Smav}
88195801Smav
89195801Smavint
90195801Smav_kvm_initvtop(kvm_t *kd)
91195801Smav{
92195801Smav	struct nlist nlist[2];
93195801Smav	struct vmstate *vm;
94195801Smav	u_long pa;
95195801Smav
96195801Smav	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
97195801Smav	if (vm == NULL) {
98195801Smav		_kvm_err(kd, kd->program, "cannot allocate vm");
99195801Smav		return (-1);
100195801Smav	}
101195801Smav	kd->vmst = vm;
102195801Smav	vm->vm_tsb = 0;
103195801Smav
104195801Smav	nlist[0].n_name = "tsb_kernel_phys";
105195801Smav	nlist[1].n_name = 0;
106195801Smav
107195801Smav	if (kvm_nlist(kd, nlist) != 0) {
108195801Smav		_kvm_err(kd, kd->program, "bad namelist");
109195801Smav		return (-1);
110195801Smav	}
111195801Smav	if (kvm_read(kd, nlist[0].n_value, &pa, sizeof(pa)) != sizeof(pa)) {
112195801Smav		_kvm_err(kd, kd->program, "cannot read tsb_kernel_phys");
113195801Smav		return (-1);
114195801Smav	}
115195801Smav	vm->vm_tsb = pa;
116195801Smav	return (0);
117195801Smav}
118195801Smav
119195801Smavint
120195801Smav_kvm_kvatop(kvm_t *kd, u_long va, u_long *pa)
121195801Smav{
122195801Smav	struct vmstate *vm;
123195801Smav	struct tte tte;
124195801Smav	u_long offset;
125195801Smav	u_long tte_pa;
126195801Smav	u_long vpn;
127195801Smav
128195801Smav	vpn = btop(va);
129195801Smav	offset = va & PAGE_MASK;
130195801Smav	tte_pa = kd->vmst->vm_tsb + ((vpn & TSB_KERNEL_MASK) << TTE_SHIFT);
131195801Smav
132195801Smav	/* XXX This has to be a physical address read, kvm_read is virtual */
133195801Smav	if (lseek(kd->pmfd, tte_pa, 0) == -1) {
134195801Smav		_kvm_syserr(kd, kd->program, "_kvm_vatop: lseek");
135195801Smav		goto invalid;
136195801Smav	}
137195801Smav	if (read(kd->pmfd, &tte, sizeof(tte)) != sizeof(tte)) {
138195801Smav		_kvm_syserr(kd, kd->program, "_kvm_vatop: read");
139195801Smav		goto invalid;
140195801Smav	}
141195801Smav	if (!tte_match(tte, va))
142195801Smav		goto invalid;
143195801Smav
144195801Smav	*pa = TD_PA(tte.tte_data) + offset;
145195801Smav	return (PAGE_SIZE - offset);
146195801Smav
147195801Smavinvalid:
148195801Smav	_kvm_err(kd, 0, "invalid address (%x)", va);
149195801Smav	return (0);
150195801Smav}
151195801Smav