1 /* $NetBSD: kobj_machdep.c,v 1.7 2009/01/08 01:03:24 pooka Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software developed for The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright 1996-1998 John D. Polstra. 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 55 */ 56 57 #include <sys/cdefs.h> 58 __KERNEL_RCSID(0, "$NetBSD: kobj_machdep.c,v 1.7 2009/01/08 01:03:24 pooka Exp $"); 59 60 #define ELFSIZE ARCH_ELFSIZE 61 62 #include <sys/param.h> 63 #include <sys/systm.h> 64 #include <sys/kernel.h> 65 #include <sys/kobj.h> 66 #include <sys/exec.h> 67 #include <sys/exec_elf.h> 68 #include <sys/xcall.h> 69 70 #include <machine/cpufunc.h> 71 72 int 73 kobj_reloc(kobj_t ko, uintptr_t relocbase, const void *data, 74 bool isrela, bool local) 75 { 76 Elf_Addr *where; 77 Elf_Addr addr; 78 Elf_Addr addend; 79 Elf_Word rtype, symidx; 80 const Elf_Rel *rel; 81 const Elf_Rela *rela; 82 83 if (isrela) { 84 rela = (const Elf_Rela *)data; 85 where = (Elf_Addr *) (relocbase + rela->r_offset); 86 addend = rela->r_addend; 87 rtype = ELF_R_TYPE(rela->r_info); 88 symidx = ELF_R_SYM(rela->r_info); 89 } else { 90 rel = (const Elf_Rel *)data; 91 where = (Elf_Addr *) (relocbase + rel->r_offset); 92 addend = *where; 93 rtype = ELF_R_TYPE(rel->r_info); 94 symidx = ELF_R_SYM(rel->r_info); 95 } 96 97 switch (rtype) { 98 case R_386_NONE: /* none */ 99 return 0; 100 101 case R_386_32: /* S + A */ 102 addr = kobj_sym_lookup(ko, symidx); 103 if (addr == 0) 104 return -1; 105 addr += addend; 106 break; 107 108 case R_386_PC32: /* S + A - P */ 109 addr = kobj_sym_lookup(ko, symidx); 110 if (addr == 0) 111 return -1; 112 addr += addend - (Elf_Addr)where; 113 break; 114 115 case R_386_GLOB_DAT: /* S */ 116 addr = kobj_sym_lookup(ko, symidx); 117 if (addr == 0) 118 return -1; 119 break; 120 121 case R_386_RELATIVE: 122 addr = relocbase + addend; 123 break; 124 125 default: 126 printf("kobj_reloc: unexpected relocation type %d\n", rtype); 127 return -1; 128 } 129 130 *where = addr; 131 return 0; 132 } 133 134 int 135 kobj_machdep(kobj_t ko, void *base, size_t size, bool load) 136 { 137 uint64_t where; 138 139 /* 140 * Currently we want this to invalidate the Pentium 4 trace cache. 141 * Other caches are snoopably coherent. 142 */ 143 if (load) { 144 if (cold) { 145 wbinvd(); 146 } else { 147 where = xc_broadcast(0, (xcfunc_t)wbinvd, NULL, NULL); 148 xc_wait(where); 149 } 150 } 151 152 return 0; 153 } 154