_set_tp.c revision 133754
1133754Sdfr/*- 2133754Sdfr * Copyright (c) 2004 Doug Rabson 3133754Sdfr * All rights reserved. 4133754Sdfr * 5133754Sdfr * Redistribution and use in source and binary forms, with or without 6133754Sdfr * modification, are permitted provided that the following conditions 7133754Sdfr * are met: 8133754Sdfr * 1. Redistributions of source code must retain the above copyright 9133754Sdfr * notice, this list of conditions and the following disclaimer. 10133754Sdfr * 2. Redistributions in binary form must reproduce the above copyright 11133754Sdfr * notice, this list of conditions and the following disclaimer in the 12133754Sdfr * documentation and/or other materials provided with the distribution. 13133754Sdfr * 14133754Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15133754Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16133754Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17133754Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18133754Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19133754Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20133754Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21133754Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22133754Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23133754Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24133754Sdfr * SUCH DAMAGE. 25133754Sdfr * 26133754Sdfr * $FreeBSD: head/lib/libc/i386/gen/_set_tp.c 133754 2004-08-15 16:18:52Z dfr $ 27133754Sdfr */ 28133754Sdfr 29133754Sdfr#include <string.h> 30133754Sdfr#include <stdint.h> 31133754Sdfr#include <machine/segments.h> 32133754Sdfr#include <machine/sysarch.h> 33133754Sdfr 34133754Sdfrvoid 35133754Sdfr_set_tp(void *tp) 36133754Sdfr{ 37133754Sdfr union descriptor ldt; 38133754Sdfr int sel; 39133754Sdfr 40133754Sdfr memset(&ldt, 0, sizeof(ldt)); 41133754Sdfr ldt.sd.sd_lolimit = 0xffff; /* 4G limit */ 42133754Sdfr ldt.sd.sd_lobase = ((uintptr_t)tp) & 0xffffff; 43133754Sdfr ldt.sd.sd_type = SDT_MEMRWA; 44133754Sdfr ldt.sd.sd_dpl = SEL_UPL; 45133754Sdfr ldt.sd.sd_p = 1; /* present */ 46133754Sdfr ldt.sd.sd_hilimit = 0xf; /* 4G limit */ 47133754Sdfr ldt.sd.sd_def32 = 1; /* 32 bit */ 48133754Sdfr ldt.sd.sd_gran = 1; /* limit in pages */ 49133754Sdfr ldt.sd.sd_hibase = (((uintptr_t)tp) >> 24) & 0xff; 50133754Sdfr sel = i386_set_ldt(LDT_AUTO_ALLOC, &ldt, 1); 51133754Sdfr __asm __volatile("movl %0,%%gs" : : "rm" ((sel << 3) | 7)); 52133754Sdfr} 53