_set_tp.c revision 133754
1230557Sjimharris/*- 2230557Sjimharris * Copyright (c) 2004 Doug Rabson 3230557Sjimharris * All rights reserved. 4230557Sjimharris * 5230557Sjimharris * Redistribution and use in source and binary forms, with or without 6230557Sjimharris * modification, are permitted provided that the following conditions 7230557Sjimharris * are met: 8230557Sjimharris * 1. Redistributions of source code must retain the above copyright 9230557Sjimharris * notice, this list of conditions and the following disclaimer. 10230557Sjimharris * 2. Redistributions in binary form must reproduce the above copyright 11230557Sjimharris * notice, this list of conditions and the following disclaimer in the 12230557Sjimharris * documentation and/or other materials provided with the distribution. 13230557Sjimharris * 14230557Sjimharris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15230557Sjimharris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16230557Sjimharris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17230557Sjimharris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18230557Sjimharris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19230557Sjimharris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20230557Sjimharris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21230557Sjimharris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22230557Sjimharris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23230557Sjimharris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24230557Sjimharris * SUCH DAMAGE. 25230557Sjimharris * 26230557Sjimharris * $FreeBSD: head/lib/libc/i386/gen/_set_tp.c 133754 2004-08-15 16:18:52Z dfr $ 27230557Sjimharris */ 28230557Sjimharris 29230557Sjimharris#include <string.h> 30230557Sjimharris#include <stdint.h> 31230557Sjimharris#include <machine/segments.h> 32230557Sjimharris#include <machine/sysarch.h> 33230557Sjimharris 34230557Sjimharrisvoid 35230557Sjimharris_set_tp(void *tp) 36230557Sjimharris{ 37230557Sjimharris union descriptor ldt; 38230557Sjimharris int sel; 39230557Sjimharris 40230557Sjimharris memset(&ldt, 0, sizeof(ldt)); 41230557Sjimharris ldt.sd.sd_lolimit = 0xffff; /* 4G limit */ 42230557Sjimharris ldt.sd.sd_lobase = ((uintptr_t)tp) & 0xffffff; 43230557Sjimharris ldt.sd.sd_type = SDT_MEMRWA; 44230557Sjimharris ldt.sd.sd_dpl = SEL_UPL; 45230557Sjimharris ldt.sd.sd_p = 1; /* present */ 46230557Sjimharris ldt.sd.sd_hilimit = 0xf; /* 4G limit */ 47230557Sjimharris ldt.sd.sd_def32 = 1; /* 32 bit */ 48230557Sjimharris ldt.sd.sd_gran = 1; /* limit in pages */ 49230557Sjimharris ldt.sd.sd_hibase = (((uintptr_t)tp) >> 24) & 0xff; 50230557Sjimharris sel = i386_set_ldt(LDT_AUTO_ALLOC, &ldt, 1); 51230557Sjimharris __asm __volatile("movl %0,%%gs" : : "rm" ((sel << 3) | 7)); 52230557Sjimharris} 53230557Sjimharris