sysi86.c revision ae115bc77f6fcde83175c75b4206dc2e50747966
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
/* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
/* All Rights Reserved */
/* Copyright (c) 1987, 1988 Microsoft Corporation */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/sysmacros.h>
#include <sys/privregs.h>
#include <sys/x86_archext.h>
#include <sys/archsystm.h>
#include <vm/seg_kmem.h>
#include <vm/faultcode.h>
#include <sys/segments.h>
/*
* sysi86 System Call
*/
/* ARGSUSED */
int
{
int error = 0;
int c;
switch (cmd) {
/*
* The SI86V86 subsystem call of the SYSI86 system call
* supports only one subcode -- V86SC_IOPL.
*/
case SI86V86:
if (arg1 == V86SC_IOPL) {
/*
* Must be privileged to run this system call
* if giving more io privilege.
*/
} else
break;
/*
* Set a segment descriptor
*/
case SI86DSCR:
/*
* There are considerable problems here manipulating
* resources shared by many running lwps. Get everyone
* into a safe state before changing the LDT.
*/
break;
}
if (get_udatamodel() == DATAMODEL_LP64) {
break;
}
break;
}
break;
case SI86FPHW:
c = fp_kind & 0xff;
break;
case SI86FPSTART:
/*
* arg1 is the address of _fp_hw
* arg2 is the desired x87 FCW value
* arg3 is the desired SSE MXCSR value
* a return value of one means SSE hardware, else none.
*/
c = fp_kind & 0xff;
break;
}
/* real time clock management commands */
case WTODC:
gethrestime(&ts);
}
break;
/* Give some timezone playing room */
case SGMTL:
/*
* Called from 32 bit land, negative values
* are not sign extended, so we do that here
* by casting it to an int and back. We also
* clamp the value to within reason and detect
* when a 64 bit call overflows an int.
*/
#ifdef _SYSCALL32_IMPL
if (get_udatamodel() == DATAMODEL_NATIVE &&
} else
#endif
else
}
break;
case GGMTL:
if (get_udatamodel() == DATAMODEL_NATIVE) {
#ifdef _SYSCALL32_IMPL
} else {
/*
* Since gmt_lag can at most be
* +/- 12 hours, something is
* *seriously* messed up here.
*/
#endif
}
break;
case RTCSYNC:
rtcsync();
break;
/* END OF real time clock management commands */
default:
break;
}
}
void
{
/*
* set type, dpl and present bits.
*/
/*
* set avl, DB and granularity bits.
*/
#if defined(__amd64)
#else
#endif
}
static void
{
/*
* set type, dpl and present bits.
*/
/*
* set avl, DB and granularity bits.
*/
#if defined(__amd64)
#else
#endif
}
static void
{
/*
* set type, dpl and present bits.
*/
#if defined(__i386) /* reserved, ignored in amd64 */
sgd->sgd_stkcpy = 0;
#endif
}
/*
* Load LDT register with the current process's LDT.
*/
void
ldt_load(void)
{
/*
*/
}
/*
* Store a NULL selector in the LDTR. All subsequent illegal references to
* the LDT will result in a #gp.
*/
void
ldt_unload(void)
{
wr_ldtr(0);
}
/*ARGSUSED*/
static void
ldt_savectx(proc_t *p)
{
#if defined(__amd64)
/*
* The 64-bit kernel must be sure to clear any stale ldt
* selectors when context switching away from a process that
* has a private ldt. Consider the following example:
*
* Wine creats a ldt descriptor and points a segment register
* to it.
*
* We then context switch away from wine lwp to kernel
* thread and hit breakpoint in kernel with kmdb
*
* When we continue and resume from kmdb we will #gp
* fault since kmdb will have saved the stale ldt selector
* from wine and will try to restore it but we are no longer in
* the context of the wine process and do not have our
* ldtr register pointing to the private ldt.
*/
#endif
ldt_unload();
}
static void
ldt_restorectx(proc_t *p)
{
ldt_load();
}
/*
* When a process with a private LDT execs, fast syscalls must be enabled for
* the new process image.
*/
/* ARGSUSED */
static void
{
if (isexec) {
}
/*
* ldt_free() will free the memory used by the private LDT, reset the
* process's descriptor, and re-program the LDTR.
*/
ldt_free(p);
}
/*
* See comments below.
*
* When a thread with a private LDT forks, the new process
* must have the LDT context ops installed.
*/
/* ARGSUSED */
static void
{
kthread_t *t;
/*
* If this is a fork, operate on the child process.
*/
}
/*
* The process context ops expect the target process as their argument.
*/
/*
* We've just disabled fast system call and return instructions; take
* the slow path out to make sure we don't try to use one to return
* back to user. We must set t_post_sys for every thread in the
* process to make sure none of them escape out via fast return.
*/
do {
t->t_post_sys = 1;
}
int
{
/*
* LDT segments: executable and data at DPL 3 only.
*/
return (EINVAL);
/*
* check the selector index.
*/
return (EINVAL);
/*
* If this is the first time for this process then setup a
* private LDT for it.
*/
/*
* Now that this process has a private LDT, the use of
* is forbidden for this processes because they destroy
* the contents of %cs and %ss segment registers.
*
* Explicity disable them here and add a context handler
* to the process. Note that disabling
* them here means we can't use sysret or sysexit on
* the way out of this system call - so we force this
* thread to take the slow path (which doesn't make use
* of sysenter or sysexit) back out.
*/
}
return (ENOMEM);
}
/*
* On the 64-bit kernel, this is where things get more subtle.
* Recall that in the 64-bit kernel, when we enter the kernel we
* deliberately -don't- reload the segment selectors we came in on
* for %ds, %es, %fs or %gs. Messing with selectors is expensive,
* and the underlying descriptors are essentially ignored by the
* hardware in long mode - except for the base that we override with
* the gsbase MSRs.
*
* However, there's one unfortunate issue with this rosy picture --
* a descriptor that's not marked as 'present' will still generate
* an #np when loading a segment register.
*
* Consider this case. An lwp creates a harmless LDT entry, points
* one of it's segment registers at it, then tells the kernel (here)
* to delete it. In the 32-bit kernel, the #np will happen on the
* way back to userland where we reload the segment registers, and be
* handled in kern_gpfault(). In the 64-bit kernel, the same thing
* will happen in the normal case too. However, if we're trying to
* use a debugger that wants to save and restore the segment registers,
* and the debugger things that we have valid segment registers, we
* have the problem that the debugger will try and restore the
* segment register that points at the now 'not present' descriptor
* and will take a #np right there.
*
* We should obviously fix the debugger to be paranoid about
* -not- restoring segment registers that point to bad descriptors;
* however we can prevent the problem here if we check to see if any
* of the segment registers are still pointing at the thing we're
* destroying; if they are, return an error instead. (That also seems
* a lot better failure mode than SIGKILL and a core file
* from kern_gpfault() too.)
*/
if (SI86SSD_PRES(ssd) == 0) {
kthread_t *t;
int bad = 0;
/*
* Look carefully at the segment registers of every lwp
* in the process (they're all stopped by our caller).
* If we're about to invalidate a descriptor that's still
* being referenced by *any* of them, return an error,
* rather than having them #gp on their way out of the kernel.
*/
do {
#if defined(__amd64)
#endif
bad = 1;
break;
}
#if defined(__amd64)
bad = 1;
break;
}
} else
#endif
{
bad = 1;
break;
}
}
if (bad) {
return (EBUSY);
}
}
/*
* If acc1 is zero, clear the descriptor (including the 'present' bit)
*/
return (0);
}
/*
* Check segment type, allow segment not present and
* only user DPL (3).
*/
return (EINVAL);
}
#if defined(__amd64)
/*
* Do not allow 32-bit applications to create 64-bit mode code
* segments.
*/
SI86SSD_ISLONG(ssd)) {
return (EINVAL);
}
#endif /* __amd64 */
/*
* Set up a code or data user segment descriptor.
*/
if (SI86SSD_ISUSEG(ssd)) {
return (0);
}
/*
* Allow a call gate only if the destination is in the LDT.
*/
return (0);
}
return (EINVAL);
}
/*
* Allocate a private LDT for this process and initialize it with the
* default entries.
*/
static void
{
/*
* Allocate maximum virtual space we need for this LDT.
*/
/*
* Allocate the minimum number of physical pages for LDT.
*/
/* Update proc structure. XXX - need any locks here??? */
}
/*
* Map the page corresponding to the selector entry. If the page is
* already mapped then it simply returns with the pointer to the entry.
* Otherwise it allocates a physical page for it and returns the pointer
* to the entry. Returns 0 for errors.
*/
static void *
{
return (ent_addr);
/*
* We are increasing the size of the process's LDT.
* Make sure this and all intervening pages are mapped.
*/
(void) *(volatile int *)page; /* peek at the page */
else { /* Allocate a physical page */
}
no_trap();
}
/* XXX - need any locks to update proc_t or gdt ??? */
ldt_load();
return (ent_addr);
}
/*
* Free up the kernel memory used for LDT of this process.
*/
static void
{
/* Free the physical page(s) used for mapping LDT */
/* peek at the address */
(void) *(volatile int *)addr;
}
}
no_trap();
/* Free up the virtual address space used for this LDT */
pp->p_ldtlimit = 0;
ldt_unload();
}
/*
* On fork copy new ldt for child.
*/
void
{
int minsize;
/* Walk thru the physical page(s) used for parent's LDT */
(void) *(volatile int *)addr; /* peek at the address */
/* allocate a page if necessary */
}
}
}
no_trap();
}