14Srgrimes/*-
2122296Speter * Copyright (c) 2003 Peter Wemm.
34Srgrimes * Copyright (c) 1990 The Regents of the University of California.
44Srgrimes * All rights reserved.
54Srgrimes *
64Srgrimes * This code is derived from software contributed to Berkeley by
74Srgrimes * William Jolitz.
84Srgrimes *
94Srgrimes * Redistribution and use in source and binary forms, with or without
104Srgrimes * modification, are permitted provided that the following conditions
114Srgrimes * are met:
124Srgrimes * 1. Redistributions of source code must retain the above copyright
134Srgrimes *    notice, this list of conditions and the following disclaimer.
144Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
154Srgrimes *    notice, this list of conditions and the following disclaimer in the
164Srgrimes *    documentation and/or other materials provided with the distribution.
174Srgrimes * 4. Neither the name of the University nor the names of its contributors
184Srgrimes *    may be used to endorse or promote products derived from this software
194Srgrimes *    without specific prior written permission.
204Srgrimes *
214Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314Srgrimes * SUCH DAMAGE.
324Srgrimes *
33557Srgrimes *	from: @(#)pcb.h	5.10 (Berkeley) 5/12/91
3450477Speter * $FreeBSD: releng/10.2/sys/amd64/include/pcb.h 271999 2014-09-22 20:34:36Z jhb $
354Srgrimes */
364Srgrimes
37122296Speter#ifndef _AMD64_PCB_H_
38122296Speter#define _AMD64_PCB_H_
39557Srgrimes
404Srgrimes/*
41122278Speter * AMD64 process control block
424Srgrimes */
43122278Speter#include <machine/fpu.h>
44168035Sjkim#include <machine/segments.h>
454Srgrimes
46258886Skib#ifdef __amd64__
474Srgrimesstruct pcb {
48114349Speter	register_t	pcb_r15;
49114349Speter	register_t	pcb_r14;
50114349Speter	register_t	pcb_r13;
51114349Speter	register_t	pcb_r12;
52114349Speter	register_t	pcb_rbp;
53114349Speter	register_t	pcb_rsp;
54114349Speter	register_t	pcb_rbx;
55114349Speter	register_t	pcb_rip;
56114928Speter	register_t	pcb_fsbase;
57114928Speter	register_t	pcb_gsbase;
58210777Sjkim	register_t	pcb_kgsbase;
59210777Sjkim	register_t	pcb_cr0;
60210777Sjkim	register_t	pcb_cr2;
61210777Sjkim	register_t	pcb_cr3;
62210777Sjkim	register_t	pcb_cr4;
63210777Sjkim	register_t	pcb_dr0;
64210777Sjkim	register_t	pcb_dr1;
65210777Sjkim	register_t	pcb_dr2;
66210777Sjkim	register_t	pcb_dr3;
67210777Sjkim	register_t	pcb_dr6;
68210777Sjkim	register_t	pcb_dr7;
6948691Sjlemon
70216634Sjkim	struct region_descriptor pcb_gdt;
71216634Sjkim	struct region_descriptor pcb_idt;
72216634Sjkim	struct region_descriptor pcb_ldt;
73216634Sjkim	uint16_t	pcb_tr;
74216634Sjkim
75216673Sjkim	u_int		pcb_flags;
76216634Sjkim#define	PCB_FULL_IRET	0x01	/* full iret is required */
77210780Sjkim#define	PCB_DBREGS	0x02	/* process using debug registers */
78210780Sjkim#define	PCB_KERNFPU	0x04	/* kernel uses fpu */
79210780Sjkim#define	PCB_FPUINITDONE	0x08	/* fpu state is initialized */
80210780Sjkim#define	PCB_USERFPUINITDONE 0x10 /* fpu user state is initialized */
81210780Sjkim#define	PCB_32BIT	0x40	/* process has 32 bit context (segs etc) */
82210780Sjkim
83189423Sjhb	uint16_t	pcb_initial_fpucw;
8448691Sjlemon
85216634Sjkim	/* copyin/out fault recovery */
86216634Sjkim	caddr_t		pcb_onfault;
87168035Sjkim
88168035Sjkim	/* 32-bit segment descriptor */
89210777Sjkim	struct user_segment_descriptor pcb_gs32sd;
90216634Sjkim
91190620Skib	/* local tss, with i/o bitmap; NULL for common */
92190620Skib	struct amd64tss *pcb_tssp;
934Srgrimes
94236772Siwasaki	/* model specific registers */
95236772Siwasaki	register_t	pcb_efer;
96236772Siwasaki	register_t	pcb_star;
97236772Siwasaki	register_t	pcb_lstar;
98236772Siwasaki	register_t	pcb_cstar;
99236772Siwasaki	register_t	pcb_sfmask;
100236772Siwasaki
101216634Sjkim	struct savefpu	*pcb_save;
102230426Skib
103271999Sjhb	uint64_t	pcb_pad[5];
104189903Sjkim};
105271999Sjhb
106271999Sjhb/* Per-CPU state saved during suspend and resume. */
107271999Sjhbstruct susppcb {
108271999Sjhb	struct pcb	sp_pcb;
109271999Sjhb
110271999Sjhb	/* fpu context for suspend/resume */
111271999Sjhb	void		*sp_fpususpend;
112271999Sjhb};
113258886Skib#endif
114189903Sjkim
11555205Speter#ifdef _KERNEL
116131905Smarcelstruct trapframe;
117131905Smarcel
118216634Sjkim/*
119216634Sjkim * The pcb_flags is only modified by current thread, or by other threads
120216634Sjkim * when current thread is stopped.  However, current thread may change it
121216634Sjkim * from the interrupt context in cpu_switch(), or in the trap handler.
122216634Sjkim * When we read-modify-write pcb_flags from C sources, compiler may generate
123216634Sjkim * code that is not atomic regarding the interrupt handler.  If a trap or
124216634Sjkim * interrupt happens and any flag is modified from the handler, it can be
125216634Sjkim * clobbered with the cached value later.  Therefore, we implement setting
126216634Sjkim * and clearing flags with single-instruction functions, which do not race
127216634Sjkim * with possible modification of the flags from the trap or interrupt context,
128216634Sjkim * because traps and interrupts are executed only on instruction boundary.
129216634Sjkim */
130216634Sjkimstatic __inline void
131216673Sjkimset_pcb_flags(struct pcb *pcb, const u_int flags)
132216634Sjkim{
133216634Sjkim
134216673Sjkim	__asm __volatile("orl %1,%0"
135216673Sjkim	    : "=m" (pcb->pcb_flags) : "ir" (flags), "m" (pcb->pcb_flags)
136216634Sjkim	    : "cc");
137216634Sjkim}
138216634Sjkim
139216634Sjkimstatic __inline void
140216673Sjkimclear_pcb_flags(struct pcb *pcb, const u_int flags)
141216634Sjkim{
142216634Sjkim
143216673Sjkim	__asm __volatile("andl %1,%0"
144216673Sjkim	    : "=m" (pcb->pcb_flags) : "ir" (~flags), "m" (pcb->pcb_flags)
145216634Sjkim	    : "cc");
146216634Sjkim}
147216634Sjkim
148131905Smarcelvoid	makectx(struct trapframe *, struct pcb *);
149234785Sdimint	savectx(struct pcb *) __returns_twice;
150237037Sjkimvoid	resumectx(struct pcb *);
151230426Skib
1524Srgrimes#endif
153557Srgrimes
154122296Speter#endif /* _AMD64_PCB_H_ */
155