--- Makefile.inc1.orig +++ Makefile.inc1 @@ -1054,9 +1054,11 @@ usr.bin/clang/clang-tblgen .endif +# dtrace tools are required for older bootstrap env and cross-build .if ${MK_CDDL} != "no" && \ - ${BOOTSTRAPPING} < 800038 && \ - !(${BOOTSTRAPPING} >= 700112 && ${BOOTSTRAPPING} < 799999) + ((${BOOTSTRAPPING} < 800038 && \ + !(${BOOTSTRAPPING} >= 700112 && ${BOOTSTRAPPING} < 799999)) \ + || (${MACHINE} != ${TARGET} || ${MACHINE_ARCH} != ${TARGET_ARCH})) _dtrace_tools= cddl/usr.bin/sgsmsg cddl/lib/libctf lib/libelf \ lib/libdwarf cddl/usr.bin/ctfconvert cddl/usr.bin/ctfmerge .endif --- /dev/null 2012-02-29 23:00:00.000000000 -0800 +++ cddl/contrib/opensolaris/lib/libdtrace/mips/dt_isadep.c 2012-02-29 23:06:15.000000000 -0800 @@ -0,0 +1,181 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License, Version 1.0 only + * (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 + * or http://www.opensolaris.org/os/licensing. + * 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 2005 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#pragma ident "%Z%%M% %I% %E% SMI" + +#include +#include +#include +#include +#include + +#include +#include + +#define OP(x) ((x) >> 30) +#define OP2(x) (((x) >> 22) & 0x07) +#define COND(x) (((x) >> 25) & 0x0f) +#define A(x) (((x) >> 29) & 0x01) + +#define OP_BRANCH 0 + +#define OP2_BPcc 0x1 +#define OP2_Bicc 0x2 +#define OP2_BPr 0x3 +#define OP2_FBPfcc 0x5 +#define OP2_FBfcc 0x6 + +/*ARGSUSED*/ +int +dt_pid_create_entry_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp, + fasttrap_probe_spec_t *ftp, const GElf_Sym *symp) +{ + ftp->ftps_type = DTFTP_ENTRY; + ftp->ftps_pc = (uintptr_t)symp->st_value; + ftp->ftps_size = (size_t)symp->st_size; + ftp->ftps_noffs = 1; + ftp->ftps_offs[0] = 0; + + if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) { + dt_dprintf("fasttrap probe creation ioctl failed: %s\n", + strerror(errno)); + return (dt_set_errno(dtp, errno)); + } + + return (1); +} + +int +dt_pid_create_return_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp, + fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, uint64_t *stret) +{ + + uint32_t *text; + int i; + int srdepth = 0; + + dt_dprintf("%s: unimplemented\n", __func__); + return (DT_PROC_ERR); + + if ((text = malloc(symp->st_size + 4)) == NULL) { + dt_dprintf("mr sparkle: malloc() failed\n"); + return (DT_PROC_ERR); + } + + if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) { + dt_dprintf("mr sparkle: Pread() failed\n"); + free(text); + return (DT_PROC_ERR); + } + + /* + * Leave a dummy instruction in the last slot to simplify edge + * conditions. + */ + text[symp->st_size / 4] = 0; + + ftp->ftps_type = DTFTP_RETURN; + ftp->ftps_pc = symp->st_value; + ftp->ftps_size = symp->st_size; + ftp->ftps_noffs = 0; + + + free(text); + if (ftp->ftps_noffs > 0) { + if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) { + dt_dprintf("fasttrap probe creation ioctl failed: %s\n", + strerror(errno)); + return (dt_set_errno(dtp, errno)); + } + } + + + return (ftp->ftps_noffs); +} + +/*ARGSUSED*/ +int +dt_pid_create_offset_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp, + fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, ulong_t off) +{ + if (off & 0x3) + return (DT_PROC_ALIGN); + + ftp->ftps_type = DTFTP_OFFSETS; + ftp->ftps_pc = (uintptr_t)symp->st_value; + ftp->ftps_size = (size_t)symp->st_size; + ftp->ftps_noffs = 1; + ftp->ftps_offs[0] = off; + + if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) { + dt_dprintf("fasttrap probe creation ioctl failed: %s\n", + strerror(errno)); + return (dt_set_errno(dtp, errno)); + } + + return (1); +} + +/*ARGSUSED*/ +int +dt_pid_create_glob_offset_probes(struct ps_prochandle *P, dtrace_hdl_t *dtp, + fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, const char *pattern) +{ + ulong_t i; + + ftp->ftps_type = DTFTP_OFFSETS; + ftp->ftps_pc = (uintptr_t)symp->st_value; + ftp->ftps_size = (size_t)symp->st_size; + ftp->ftps_noffs = 0; + + /* + * If we're matching against everything, just iterate through each + * instruction in the function, otherwise look for matching offset + * names by constructing the string and comparing it against the + * pattern. + */ + if (strcmp("*", pattern) == 0) { + for (i = 0; i < symp->st_size; i += 4) { + ftp->ftps_offs[ftp->ftps_noffs++] = i; + } + } else { + char name[sizeof (i) * 2 + 1]; + + for (i = 0; i < symp->st_size; i += 4) { + (void) sprintf(name, "%lx", i); + if (gmatch(name, pattern)) + ftp->ftps_offs[ftp->ftps_noffs++] = i; + } + } + + if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) { + dt_dprintf("fasttrap probe creation ioctl failed: %s\n", + strerror(errno)); + return (dt_set_errno(dtp, errno)); + } + + return (ftp->ftps_noffs); +} --- cddl/contrib/opensolaris/tools/ctf/cvt/ctf.c.orig +++ cddl/contrib/opensolaris/tools/ctf/cvt/ctf.c @@ -62,6 +62,18 @@ int ntholes; /* number of type holes */ }; +/* + * Macros to reverse byte order + */ +#define BSWAP_8(x) ((x) & 0xff) +#define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8)) +#define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16)) + +#define SWAP_16(x) (x) = BSWAP_16(x) +#define SWAP_32(x) (x) = BSWAP_32(x) + +static int needSwap; + /*PRINTFLIKE1*/ static void parseterminate(const char *fmt, ...) @@ -140,6 +152,11 @@ ctl.ctl_label = strtab_insert(&b->ctb_strtab, le->le_name); ctl.ctl_typeidx = le->le_idx; + if (needSwap) { + SWAP_32(ctl.ctl_label); + SWAP_32(ctl.ctl_typeidx); + } + ctf_buf_write(b, &ctl, sizeof (ctl)); return (1); @@ -152,6 +169,10 @@ ctf_buf_write(b, &id, sizeof (id)); + if (needSwap) { + SWAP_16(id); + } + debug(3, "Wrote object %s (%d)\n", (idp ? idp->ii_name : "(null)"), id); } @@ -180,10 +201,21 @@ fdata[0] = CTF_TYPE_INFO(CTF_K_FUNCTION, 1, nargs); fdata[1] = idp->ii_dtype->t_id; + + if (needSwap) { + SWAP_16(fdata[0]); + SWAP_16(fdata[1]); + } + ctf_buf_write(b, fdata, sizeof (fdata)); for (i = 0; i < idp->ii_nargs; i++) { id = idp->ii_args[i]->t_id; + + if (needSwap) { + SWAP_16(id); + } + ctf_buf_write(b, &id, sizeof (id)); } @@ -208,11 +240,25 @@ ctt->ctt_size = CTF_LSIZE_SENT; ctt->ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size); ctt->ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size); + if (needSwap) { + SWAP_32(ctt->ctt_name); + SWAP_16(ctt->ctt_info); + SWAP_16(ctt->ctt_size); + SWAP_32(ctt->ctt_lsizehi); + SWAP_32(ctt->ctt_lsizelo); + } ctf_buf_write(b, ctt, sizeof (*ctt)); } else { ctf_stype_t *cts = (ctf_stype_t *)ctt; cts->ctt_size = (ushort_t)size; + + if (needSwap) { + SWAP_32(cts->ctt_name); + SWAP_16(cts->ctt_info); + SWAP_16(cts->ctt_size); + } + ctf_buf_write(b, cts, sizeof (*cts)); } } @@ -222,6 +268,12 @@ { ctf_stype_t *cts = (ctf_stype_t *)ctt; + if (needSwap) { + SWAP_32(cts->ctt_name); + SWAP_16(cts->ctt_info); + SWAP_16(cts->ctt_size); + } + ctf_buf_write(b, cts, sizeof (*cts)); } @@ -296,6 +348,9 @@ encoding = ip->intr_fformat; data = CTF_INT_DATA(encoding, ip->intr_offset, ip->intr_nbits); + if (needSwap) { + SWAP_32(data); + } ctf_buf_write(b, &data, sizeof (data)); break; @@ -312,6 +367,11 @@ cta.cta_contents = tp->t_ardef->ad_contents->t_id; cta.cta_index = tp->t_ardef->ad_idxtype->t_id; cta.cta_nelems = tp->t_ardef->ad_nelems; + if (needSwap) { + SWAP_16(cta.cta_contents); + SWAP_16(cta.cta_index); + SWAP_32(cta.cta_nelems); + } ctf_buf_write(b, &cta, sizeof (cta)); break; @@ -341,6 +401,11 @@ offset); ctm.ctm_type = mp->ml_type->t_id; ctm.ctm_offset = mp->ml_offset; + if (needSwap) { + SWAP_32(ctm.ctm_name); + SWAP_16(ctm.ctm_type); + SWAP_16(ctm.ctm_offset); + } ctf_buf_write(b, &ctm, sizeof (ctm)); } } else { @@ -355,6 +420,14 @@ CTF_OFFSET_TO_LMEMHI(mp->ml_offset); ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(mp->ml_offset); + + if (needSwap) { + SWAP_32(ctlm.ctlm_name); + SWAP_16(ctlm.ctlm_type); + SWAP_32(ctlm.ctlm_offsethi); + SWAP_32(ctlm.ctlm_offsetlo); + } + ctf_buf_write(b, &ctlm, sizeof (ctlm)); } } @@ -377,6 +450,12 @@ offset = strtab_insert(&b->ctb_strtab, ep->el_name); cte.cte_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset); cte.cte_value = ep->el_number; + + if (needSwap) { + SWAP_32(cte.cte_name); + SWAP_32(cte.cte_value); + } + ctf_buf_write(b, &cte, sizeof (cte)); i--; } @@ -420,6 +499,11 @@ for (i = 0; i < (int) tp->t_fndef->fn_nargs; i++) { id = tp->t_fndef->fn_args[i]->t_id; + + if (needSwap) { + SWAP_16(id); + } + ctf_buf_write(b, &id, sizeof (id)); } @@ -613,6 +697,9 @@ int i; + needSwap = do_compress & CTF_SWAP_BYTES; + do_compress &= ~CTF_SWAP_BYTES; + /* * Prepare the header, and create the CTF output buffers. The data * object section and function section are both lists of 2-byte @@ -649,6 +736,18 @@ h.cth_stroff = ctf_buf_cur(buf); h.cth_strlen = strtab_size(&buf->ctb_strtab); + if (needSwap) { + SWAP_16(h.cth_preamble.ctp_magic); + SWAP_32(h.cth_parlabel); /* ref to name of parent lbl uniq'd against */ + SWAP_32(h.cth_parname); /* ref to basename of parent */ + SWAP_32(h.cth_lbloff); /* offset of label section */ + SWAP_32(h.cth_objtoff); /* offset of object section */ + SWAP_32(h.cth_funcoff); /* offset of function section */ + SWAP_32(h.cth_typeoff); /* offset of type section */ + SWAP_32(h.cth_stroff); /* offset of string section */ + SWAP_32(h.cth_strlen); /* length of string section in bytes */ + } + /* * We only do compression for ctfmerge, as ctfconvert is only * supposed to be used on intermediary build objects. This is --- cddl/contrib/opensolaris/tools/ctf/cvt/ctfmerge.c.orig +++ cddl/contrib/opensolaris/tools/ctf/cvt/ctfmerge.c @@ -620,7 +620,7 @@ terminate("No CTF data found in source file %s\n", srcfile); tmpname = mktmpname(destfile, ".ctf"); - write_ctf(srctd, destfile, tmpname, CTF_COMPRESS | keep_stabs); + write_ctf(srctd, destfile, tmpname, CTF_COMPRESS | CTF_SWAP_BYTES | keep_stabs); if (rename(tmpname, destfile) != 0) { terminate("Couldn't rename temp file %s to %s", tmpname, destfile); @@ -1015,7 +1015,7 @@ tmpname = mktmpname(outfile, ".ctf"); write_ctf(savetd, outfile, tmpname, - CTF_COMPRESS | write_fuzzy_match | dynsym | keep_stabs); + CTF_COMPRESS | CTF_SWAP_BYTES | write_fuzzy_match | dynsym | keep_stabs); if (rename(tmpname, outfile) != 0) terminate("Couldn't rename output temp file %s", tmpname); free(tmpname); --- cddl/contrib/opensolaris/tools/ctf/cvt/ctftools.h.orig +++ cddl/contrib/opensolaris/tools/ctf/cvt/ctftools.h @@ -391,6 +391,7 @@ #define CTF_USE_DYNSYM 0x2 /* use .dynsym not .symtab */ #define CTF_COMPRESS 0x4 /* compress CTF output */ #define CTF_KEEP_STABS 0x8 /* keep .stabs sections */ +#define CTF_SWAP_BYTES 0x10 /* target byte order is different from host */ void write_ctf(tdata_t *, const char *, const char *, int); --- cddl/contrib/opensolaris/tools/ctf/cvt/output.c.orig +++ cddl/contrib/opensolaris/tools/ctf/cvt/output.c @@ -717,7 +717,7 @@ iiburst = sort_iidescs(elf, file, td, flags & CTF_FUZZY_MATCH, flags & CTF_USE_DYNSYM); - data = ctf_gen(iiburst, lenp, flags & CTF_COMPRESS); + data = ctf_gen(iiburst, lenp, flags & (CTF_COMPRESS | CTF_SWAP_BYTES)); iiburst_free(iiburst); @@ -730,10 +730,12 @@ struct stat st; Elf *elf = NULL; Elf *telf = NULL; + GElf_Ehdr ehdr; caddr_t data; size_t len; int fd = -1; int tfd = -1; + int byteorder; (void) elf_version(EV_CURRENT); if ((fd = open(curname, O_RDONLY)) < 0 || fstat(fd, &st) < 0) @@ -746,6 +748,22 @@ if ((telf = elf_begin(tfd, ELF_C_WRITE, NULL)) == NULL) elfterminate(curname, "Cannot write"); + if (gelf_getehdr(elf, &ehdr)) { +#if BYTE_ORDER == _BIG_ENDIAN + byteorder = ELFDATA2MSB; +#else + byteorder = ELFDATA2LSB; +#endif + /* + * If target and host has the same byte order + * clear byte swapping request + */ + if (ehdr.e_ident[EI_DATA] == byteorder) + flags &= ~CTF_SWAP_BYTES; + } + else + elfterminate(curname, "Failed to get EHDR"); + data = make_ctf_data(td, elf, curname, &len, flags); write_file(elf, curname, telf, newname, data, len, flags); free(data); --- cddl/lib/Makefile.orig +++ cddl/lib/Makefile @@ -19,7 +19,7 @@ .endif .endif -.if ${MACHINE_ARCH} == "amd64" || ${MACHINE_ARCH} == "i386" +.if ${MACHINE_ARCH} == "amd64" || ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "mips64eb" _drti= drti _libdtrace= libdtrace .endif --- cddl/lib/libdtrace/Makefile.orig +++ cddl/lib/libdtrace/Makefile @@ -42,8 +42,7 @@ dt_subr.c \ dt_work.c \ dt_xlator.c \ - gmatch.c \ - dis_tables.c + gmatch.c DSRCS= errno.d \ psinfo.d \ @@ -70,12 +69,17 @@ .elif ${MACHINE_CPUARCH} == "sparc64" CFLAGS+= -I${OPENSOLARIS_SYS_DISTDIR}/uts/sparc .PATH: ${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libdtrace/sparc +.elif ${MACHINE_CPUARCH} == "mips" +CFLAGS+= -I${OPENSOLARIS_SYS_DISTDIR}/uts/mips +.PATH: ${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libdtrace/mips +.PATH: ${.CURDIR}/../../../sys/cddl/dev/dtrace/mips .else # temporary hack CFLAGS+= -I${OPENSOLARIS_SYS_DISTDIR}/uts/intel .endif .if ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "amd64" +SRCS+= dis_tables.c DSRCS+= regs_x86.d .endif --- cddl/usr.sbin/Makefile.orig +++ cddl/usr.sbin/Makefile @@ -19,4 +19,8 @@ _lockstat= lockstat .endif +.if ${MACHINE_CPUARCH} == "mips" +_dtrace= dtrace +.endif + .include --- contrib/gcc/config/mips/freebsd.h.orig +++ contrib/gcc/config/mips/freebsd.h @@ -351,4 +351,5 @@ #endif /************************[ Debugger stuff ]*********************************/ - +#undef DBX_DEBUGGING_INFO +#undef MIPS_DEBUGGING_INFO --- gnu/usr.bin/binutils/as/Makefile.orig +++ gnu/usr.bin/binutils/as/Makefile @@ -43,6 +43,13 @@ .if ${TARGET_CPUARCH} == "mips" SRCS+= itbl-ops.c itbl-parse.y itbl-lex.l +.if ${TARGET_ARCH:Mmips64*} != "" +CFLAGS+= -DMIPS_DEFAULT_ABI=N64_ABI -DMIPS_DEFAULT_64BIT=1 +.elif ${TARGET_ARCH:Mmipsn32*} != "" +CFLAGS+= -DMIPS_DEFAULT_ABI=N32_ABI +.else +MIPS_ABI_DEFAULT=ABI_32 +.endif .endif .if ${TARGET_ARCH} == "amd64" --- gnu/usr.bin/binutils/as/mips-freebsd/itbl-cpu.h.orig +++ gnu/usr.bin/binutils/as/mips-freebsd/itbl-cpu.h @@ -3,14 +3,17 @@ #include "itbl-mips.h" /* Choose a default ABI for MIPS targets. */ -/* XXX: Where should this be ? */ +#ifndef MIPS_DEFAULT_ABI #define MIPS_DEFAULT_ABI NO_ABI +#endif /* Default CPU for MIPS targets. */ #define MIPS_CPU_STRING_DEFAULT "from-abi" /* Generate 64-bit code by default on MIPS targets. */ +#ifndef MIPS_DEFAULT_64BIT #define MIPS_DEFAULT_64BIT 0 +#endif /* Allow use of E_MIPS_ABI_O32 on MIPS targets. */ #define USE_E_MIPS_ABI_O32 1 --- lib/Makefile.orig +++ lib/Makefile @@ -179,7 +179,7 @@ _libypclnt= libypclnt .endif -.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64" +.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "mips" .if ${MK_NCP} != "no" _libncp= libncp .endif --- lib/libelf/libelf_data.c.orig +++ lib/libelf/libelf_data.c @@ -86,6 +86,8 @@ #endif case SHT_MIPS_DWARF: /* FALLTHROUGH */ + case SHT_MIPS_OPTIONS: + /* FALLTHROUGH */ case SHT_AMD64_UNWIND: /* == SHT_IA_64_UNWIND */ return (ELF_T_BYTE); default: --- lib/libproc/proc_bkpt.c.orig +++ lib/libproc/proc_bkpt.c @@ -44,6 +44,9 @@ #if defined(__i386__) || defined(__amd64__) #define BREAKPOINT_INSTR 0xcc /* int 0x3 */ #define BREAKPOINT_INSTR_SZ 1 +#elif defined(__mips__) +#define BREAKPOINT_INSTR 0xd /* break */ +#define BREAKPOINT_INSTR_SZ 4 #else #error "Add support for your architecture" #endif --- lib/libproc/proc_regs.c.orig +++ lib/libproc/proc_regs.c @@ -58,6 +58,8 @@ *regvalue = regs.r_rip; #elif defined(__i386__) *regvalue = regs.r_eip; +#elif defined(__mips__) + *regvalue = regs.r_regs[PC]; #endif break; case REG_SP: @@ -65,6 +67,8 @@ *regvalue = regs.r_rsp; #elif defined(__i386__) *regvalue = regs.r_esp; +#elif defined(__mips__) + *regvalue = regs.r_regs[SP]; #endif break; default: @@ -93,6 +97,8 @@ regs.r_rip = regvalue; #elif defined(__i386__) regs.r_eip = regvalue; +#elif defined(__mips__) + regs.r_regs[PC] = regvalue; #endif break; case REG_SP: @@ -100,6 +106,8 @@ regs.r_rsp = regvalue; #elif defined(__i386__) regs.r_esp = regvalue; +#elif defined(__mips__) + regs.r_regs[PC] = regvalue; #endif break; default: --- sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c.orig +++ sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c @@ -235,7 +235,7 @@ static struct mtx dtrace_unr_mtx; MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF); int dtrace_in_probe; /* non-zero if executing a probe */ -#if defined(__i386__) || defined(__amd64__) +#if defined(__i386__) || defined(__amd64__) || defined(__mips__) uintptr_t dtrace_in_probe_addr; /* Address of invop when already in probe */ #endif #endif @@ -10659,7 +10659,7 @@ #else int i; -#if defined(__amd64__) +#if defined(__amd64__) || defined(__mips__) /* * FreeBSD isn't good at limiting the amount of memory we * ask to malloc, so let's place a limit here before trying --- /dev/null 2012-02-29 23:00:00.000000000 -0800 +++ sys/cddl/contrib/opensolaris/uts/mips/dtrace/fasttrap_isa.c 2012-02-29 23:06:15.000000000 -0800 @@ -0,0 +1,30 @@ +/* + * 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 + * or http://www.opensolaris.org/os/licensing. + * 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. + */ + + +/* + * XXX: Placeholder for MISP fasttrap code + */ --- /dev/null 2012-02-29 23:00:00.000000000 -0800 +++ sys/cddl/contrib/opensolaris/uts/mips/sys/fasttrap_isa.h 2012-02-29 23:06:15.000000000 -0800 @@ -0,0 +1,94 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License, Version 1.0 only + * (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 + * or http://www.opensolaris.org/os/licensing. + * 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 2005 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _FASTTRAP_ISA_H +#define _FASTTRAP_ISA_H + +#pragma ident "%Z%%M% %I% %E% SMI" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * This is our reserved trap instruction: ta 0x38 + */ +#define FASTTRAP_INSTR 0x91d02038 + +#define FASTTRAP_SUNWDTRACE_SIZE 128 + +typedef uint32_t fasttrap_instr_t; + +typedef struct fasttrap_machtp { + fasttrap_instr_t ftmt_instr; /* original instruction */ + uintptr_t ftmt_dest; /* destination of DCTI */ + uint8_t ftmt_type; /* emulation type */ + uint8_t ftmt_flags; /* emulation flags */ + uint8_t ftmt_cc; /* which cc to look at */ + uint8_t ftmt_code; /* branch condition */ +} fasttrap_machtp_t; + +#define ftt_instr ftt_mtp.ftmt_instr +#define ftt_dest ftt_mtp.ftmt_dest +#define ftt_type ftt_mtp.ftmt_type +#define ftt_flags ftt_mtp.ftmt_flags +#define ftt_cc ftt_mtp.ftmt_cc +#define ftt_code ftt_mtp.ftmt_code + +#define FASTTRAP_T_COMMON 0x00 /* common case -- no emulation */ +#define FASTTRAP_T_CCR 0x01 /* integer condition code branch */ +#define FASTTRAP_T_FCC 0x02 /* floating-point branch */ +#define FASTTRAP_T_REG 0x03 /* register predicated branch */ +#define FASTTRAP_T_ALWAYS 0x04 /* branch always */ +#define FASTTRAP_T_CALL 0x05 /* call instruction */ +#define FASTTRAP_T_JMPL 0x06 /* jmpl instruction */ +#define FASTTRAP_T_RDPC 0x07 /* rdpc instruction */ +#define FASTTRAP_T_RETURN 0x08 /* return instruction */ + +/* + * For performance rather than correctness. + */ +#define FASTTRAP_T_SAVE 0x10 /* save instruction (func entry only) */ +#define FASTTRAP_T_RESTORE 0x11 /* restore instruction */ +#define FASTTRAP_T_OR 0x12 /* mov instruction */ +#define FASTTRAP_T_SETHI 0x13 /* sethi instruction (includes nop) */ + +#define FASTTRAP_F_ANNUL 0x01 /* branch is annulled */ +#define FASTTRAP_F_RETMAYBE 0x02 /* not definitely a return site */ + +#define FASTTRAP_AFRAMES 3 +#define FASTTRAP_RETURN_AFRAMES 4 +#define FASTTRAP_ENTRY_AFRAMES 3 +#define FASTTRAP_OFFSET_AFRAMES 3 + + +#ifdef __cplusplus +} +#endif + +#endif /* _FASTTRAP_ISA_H */ --- /dev/null 2012-02-29 23:00:00.000000000 -0800 +++ sys/cddl/dev/dtrace/mips/dtrace_asm.S 2012-02-29 23:06:16.000000000 -0800 @@ -0,0 +1,303 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License, Version 1.0 only + * (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 + * or http://www.opensolaris.org/os/licensing. + * 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 + * + * $FreeBSD$ + */ +/* + * Copyright 2004 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#define _ASM +#define _LOCORE +#define LOCORE + +#include +#include + +#include +#include +#include +#include + +#include "assym.s" + + .set noreorder # Noreorder is default style! + +/* + * Primitives + */ + + .text + +/* +void dtrace_membar_producer(void) +*/ +LEAF(dtrace_membar_producer) + j ra + nop +END(dtrace_membar_producer) + +/* +void dtrace_membar_consumer(void) +*/ +LEAF(dtrace_membar_consumer) + j ra + nop +END(dtrace_membar_consumer) + +/* +dtrace_icookie_t dtrace_interrupt_disable(void) +*/ +LEAF(dtrace_interrupt_disable) + mfc0 t0, MIPS_COP_0_STATUS + move v0, t0 + and v0, v0, MIPS_SR_INT_IE + and t0, t0, ~MIPS_SR_INT_IE + mtc0 t0, MIPS_COP_0_STATUS + j ra + nop +END(dtrace_interrupt_disable) + +/* +void dtrace_interrupt_enable(dtrace_icookie_t cookie) +*/ +LEAF(dtrace_interrupt_enable) + mfc0 t0, MIPS_COP_0_STATUS + beqz a0, not_enabled + or t0, t0, MIPS_SR_INT_IE + mtc0 t0, MIPS_COP_0_STATUS +not_enabled: + j ra + nop +END(dtrace_interrupt_enable) + +/* +uint32_t dtrace_cas32(uint32_t *target, uint32_t cmp, uint32_t new) +*/ +LEAF(dtrace_cas32) +1: + move t1, a2 + ll t0, 0(a0) + bne t0, a1, 2f + nop + sc t1, 0(a0) + beqz t1, 1b + nop +2: move v0, t0 + j ra + nop +END(dtrace_cas32) + +/* +void * +dtrace_casptr(void *target, void *cmp, void *new) +*/ +LEAF(dtrace_casptr) +1: + move t1, a2 + PTR_LL t0, 0(a0) + bne t0, a1, 2f + nop + PTR_SC t1, 0(a0) + beqz t1, 1b + nop +2: move v0, t0 + j ra + nop +END(dtrace_casptr) + + +/* +uintptr_t +dtrace_fulword(void *addr) +*/ +LEAF(dtrace_fulword) +END(dtrace_fulword) + +/* +uint8_t +dtrace_fuword8_nocheck(void *addr) +*/ +LEAF(dtrace_fuword8_nocheck) + lbu v0, 0(a0) + j ra + nop +END(dtrace_fuword8_nocheck) + +/* +uint16_t +dtrace_fuword16_nocheck(void *addr) +*/ +LEAF(dtrace_fuword16_nocheck) + lhu v0, 0(a0) + j ra + nop +END(dtrace_fuword16_nocheck) + +/* +uint32_t +dtrace_fuword32_nocheck(void *addr) +*/ +LEAF(dtrace_fuword32_nocheck) + lwu v0, 0(a0) + j ra + nop +END(dtrace_fuword32_nocheck) + +/* +uint64_t +dtrace_fuword64_nocheck(void *addr) +*/ +LEAF(dtrace_fuword64_nocheck) +#if defined(__mips_n64) || defined(__mips_n32) + ld v0, 0(a0) +#else + lw v1,4(a0) + lw v0,0(a0) +#endif + j ra + nop +END(dtrace_fuword64_nocheck) + +/* +XXX: unoptimized +void +dtrace_copy(uintptr_t src, uintptr_t dest, size_t size) +*/ +LEAF(dtrace_copy) +1: + beqz a2, 2f + nop + lbu t0, 0(a0) + sb t0, 0(a1) + daddu a0, a0, 1 + daddu a1, a1, 1 + dsubu a2, a2, 1 + j 1b + nop +2: + j ra + nop +END(dtrace_copy) + +/* +XXX: Unoptimized. Check for flags on page boundaries only(?) +void +dtrace_copystr(uintptr_t uaddr, uintptr_t kaddr, size_t size, + volatile uint16_t *flags) +*/ +LEAF(dtrace_copystr) +1: + lbu t0, 0(a0) + sb t0, 0(a1) + daddu a0, a0, 1 + daddu a1, a1, 1 + dsubu a2, a2, 1 + beqz t0, 2f + nop + lhu t1, (a3) + and t1, t1, CPU_DTRACE_BADADDR + bnez t1, 2f + nop + + bnez a2, 1b + nop +2: + j ra + nop +END(dtrace_copystr) + +/* +void dtrace_invop_init(void) +*/ +LEAF(dtrace_invop_init) + /* XXX: impement it properly */ + dla t0, dtrace_invop_jump_addr + /* dla t1, dtrace_invop_start */ + sd zero, 0(t0) + j ra + nop +END(dtrace_invop_init) + +/* +void dtrace_invop_uninit(void) +*/ +LEAF(dtrace_invop_uninit) + dla t0, dtrace_invop_jump_addr + sd zero, 0(t0) + j ra + nop +END(dtrace_invop_uninit) + +/* + * The panic() and cmn_err() functions invoke vpanic() as a common entry point + * into the panic code implemented in panicsys(). vpanic() is responsible + * for passing through the format string and arguments, and constructing a + * regs structure on the stack into which it saves the current register + * values. If we are not dying due to a fatal trap, these registers will + * then be preserved in panicbuf as the current processor state. Before + * invoking panicsys(), vpanic() activates the first panic trigger (see + * common/os/panic.c) and switches to the panic_stack if successful. Note that + * DTrace takes a slightly different panic path if it must panic from probe + * context. Instead of calling panic, it calls into dtrace_vpanic(), which + * sets up the initial stack as vpanic does, calls dtrace_panic_trigger(), and + * branches back into vpanic(). + */ + +/* +void +vpanic(const char *format, va_list alist) +*/ +LEAF(vpanic) /* Initial stack layout: */ + +vpanic_common: + j ra + nop +END(vpanic) + + + +/* +void +dtrace_vpanic(const char *format, va_list alist) +*/ +LEAF(dtrace_vpanic) /* Initial stack layout: */ + +#if 0 + jal dtrace_panic_trigger /* %eax = dtrace_panic_trigger() */ + nop +#endif + j vpanic_common + nop + +END(dtrace_vpanic) + +/* +uintptr_t +dtrace_caller(int aframes) +*/ +LEAF(dtrace_caller) + li v0, -1 + j ra + nop +END(dtrace_caller) + --- /dev/null 2012-02-29 23:00:00.000000000 -0800 +++ sys/cddl/dev/dtrace/mips/dtrace_isa.c 2012-02-29 23:06:16.000000000 -0800 @@ -0,0 +1,685 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License, Version 1.0 only + * (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 + * or http://www.opensolaris.org/os/licensing. + * 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 + * + * $FreeBSD$ + */ +/* + * Copyright 2005 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "regset.h" + +/* + * Wee need some reasonable default to prevent backtrace code + * from wandering too far + */ +#define MAX_FUNCTION_SIZE 0x10000 +#define MAX_PROLOGUE_SIZE 0x100 + + +uint8_t dtrace_fuword8_nocheck(void *); +uint16_t dtrace_fuword16_nocheck(void *); +uint32_t dtrace_fuword32_nocheck(void *); +uint64_t dtrace_fuword64_nocheck(void *); + +static int dtrace_next_frame(register_t *pc, register_t *sp, uintptr_t *args, int *valid_args); +static int dtrace_next_uframe(register_t *pc, register_t *sp, register_t *ra); + +void +dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes, + uint32_t *intrpc) +{ + int depth = 0; + vm_offset_t callpc; + pc_t caller = (pc_t) solaris_cpu[curcpu].cpu_dtrace_caller; + register_t sp, ra, pc; + + if (intrpc != 0) + pcstack[depth++] = (pc_t) intrpc; + + aframes++; + + sp = (register_t)(intptr_t)__builtin_frame_address(0); + ra = (register_t)(intptr_t)__builtin_return_address(0); + + __asm __volatile( + "jal 99f\n" + "nop\n" + "99:\n" + "move %0, $31\n" /* get ra */ + "move $31, %1\n" /* restore ra */ + : "=r" (pc) + : "r" (ra)); + + while (depth < pcstack_limit) { + + callpc = pc; + + if (aframes > 0) { + aframes--; + if ((aframes == 0) && (caller != 0)) { + pcstack[depth++] = caller; + } + } + else { + pcstack[depth++] = callpc; + } + + if (dtrace_next_frame(&pc, &sp, NULL, NULL) < 0) + break; + } + + for (; depth < pcstack_limit; depth++) { + pcstack[depth] = 0; + } +} + +void +dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit) +{ + proc_t *p = curproc; + struct trapframe *tf; + register_t sp, ra, pc; + volatile uint16_t *flags = + (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; + + if (*flags & CPU_DTRACE_FAULT) + return; + + if (pcstack_limit <= 0) + return; + + /* + * If there's no user context we still need to zero the stack. + */ + if (p == NULL || (tf = curthread->td_frame) == NULL) + goto zero; + + *pcstack++ = (uint64_t)p->p_pid; + pcstack_limit--; + + if (pcstack_limit <= 0) + return; + + pc = (uint64_t)tf->pc; + sp = (uint64_t)tf->sp; + ra = (uint64_t)tf->ra; + *pcstack++ = (uint64_t)tf->pc; + + /* + * Unwind, and unwind, and unwind + */ + while (1) { + if (dtrace_next_uframe(&pc, &sp, &ra) < 0) + break; + + *pcstack++ = pc; + pcstack_limit--; + + if (pcstack_limit <= 0) + break; + } + +zero: + while (pcstack_limit-- > 0) + *pcstack++ = 0; +} + +int +dtrace_getustackdepth(void) +{ + int n = 0; + proc_t *p = curproc; + struct trapframe *tf; + register_t sp, ra, pc; + volatile uint16_t *flags = + (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; + + if (*flags & CPU_DTRACE_FAULT) + return (0); + + if (p == NULL || (tf = curthread->td_frame) == NULL) + return (0); + + pc = (uint64_t)tf->pc; + sp = (uint64_t)tf->sp; + ra = (uint64_t)tf->ra; + n++; + + /* + * Unwind, and unwind, and unwind + */ + while (1) { + if (dtrace_next_uframe(&pc, &sp, &ra) < 0) + break; + n++; + } + + return (n); +} + +void +dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit) +{ + printf("IMPLEMENT ME: %s\n", __func__); +} + +/*ARGSUSED*/ +uint64_t +dtrace_getarg(int arg, int aframes) +{ + int i; + register_t sp, ra, pc; + /* XXX: Fix this ugly code */ + uintptr_t args[8]; + int valid[8]; + + sp = (register_t)(intptr_t)__builtin_frame_address(0); + ra = (register_t)(intptr_t)__builtin_return_address(0); + + __asm __volatile( + "jal 99f\n" + "nop\n" + "99:\n" + "move %0, $31\n" /* get ra */ + "move $31, %1\n" /* restore ra */ + : "=r" (pc) + : "r" (ra)); + + for (i = 0; i <= aframes + 1; i++) { + if (dtrace_next_frame(&pc, &sp, args, valid) < 0) { + printf("%s: stack ends at frame #%d\n", __func__, i); + return (0); + } + } + + if (arg < 8) { + if (valid[arg]) + return (args[arg]); + else + printf("%s: request arg%d is not valid\n", __func__, arg); + } + + return (0); +} + +int +dtrace_getstackdepth(int aframes) +{ + register_t sp, ra, pc; + int depth = 0; + + sp = (register_t)(intptr_t)__builtin_frame_address(0); + ra = (register_t)(intptr_t)__builtin_return_address(0); + + __asm __volatile( + "jal 99f\n" + "nop\n" + "99:\n" + "move %0, $31\n" /* get ra */ + "move $31, %1\n" /* restore ra */ + : "=r" (pc) + : "r" (ra)); + + for (;;) { + if (dtrace_next_frame(&pc, &sp, NULL, NULL) < 0) + break; + depth++; + } + + if (depth < aframes) + return 0; + else + return depth - aframes; +} + +ulong_t +dtrace_getreg(struct trapframe *rp, uint_t reg) +{ + + return (0); +} + +static int +dtrace_next_frame(register_t *pc, register_t *sp, + uintptr_t *args, int *valid_args) +{ + InstFmt i; + /* + * Arrays for a0..a3 registers and flags if content + * of these registers is valid, e.g. obtained from the stack + */ + uintptr_t va; + unsigned instr, mask; + unsigned int frames = 0; + int more, stksize; + register_t ra = 0; + int arg, r; + + /* + * Invalidate arguments values + */ + if (valid_args) { + for (r = 0; r < 8; r++) + valid_args[r] = 0; + } + + /* Jump here after a nonstandard (interrupt handler) frame */ + stksize = 0; + if (frames++ > 100) { + /* return breaks stackframe-size heuristics with gcc -O2 */ + goto error; /* XXX */ + } + + /* check for bad SP: could foul up next frame */ + if (!MIPS_IS_VALID_KERNELADDR(*sp)) { + goto error; + } + + /* check for bad PC */ + if (!MIPS_IS_VALID_KERNELADDR(*pc)) { + goto error; + } + + /* + * Find the beginning of the current subroutine by scanning + * backwards from the current PC for the end of the previous + * subroutine. + */ + va = *pc - sizeof(int); + while (1) { + instr = kdbpeek((int *)va); + + /* [d]addiu sp,sp,-X */ + if (((instr & 0xffff8000) == 0x27bd8000) + || ((instr & 0xffff8000) == 0x67bd8000)) + break; + + /* jr ra */ + if (instr == 0x03e00008) { + /* skip over branch-delay slot instruction */ + va += 2 * sizeof(int); + break; + } + + va -= sizeof(int); + } + + /* skip over nulls which might separate .o files */ + while ((instr = kdbpeek((int *)va)) == 0) + va += sizeof(int); + + /* scan forwards to find stack size and any saved registers */ + stksize = 0; + more = 3; + mask = 0; + for (; more; va += sizeof(int), + more = (more == 3) ? 3 : more - 1) { + /* stop if hit our current position */ + if (va >= *pc) + break; + instr = kdbpeek((int *)va); + i.word = instr; + switch (i.JType.op) { + case OP_SPECIAL: + switch (i.RType.func) { + case OP_JR: + case OP_JALR: + more = 2; /* stop after next instruction */ + break; + + case OP_SYSCALL: + case OP_BREAK: + more = 1; /* stop now */ + }; + break; + + case OP_BCOND: + case OP_J: + case OP_JAL: + case OP_BEQ: + case OP_BNE: + case OP_BLEZ: + case OP_BGTZ: + more = 2; /* stop after next instruction */ + break; + + case OP_COP0: + case OP_COP1: + case OP_COP2: + case OP_COP3: + switch (i.RType.rs) { + case OP_BCx: + case OP_BCy: + more = 2; /* stop after next instruction */ + }; + break; + + case OP_SW: + /* look for saved registers on the stack */ + if (i.IType.rs != 29) + break; + /* only restore the first one */ + if (mask & (1 << i.IType.rt)) + break; + mask |= (1 << i.IType.rt); + switch (i.IType.rt) { + case 4:/* a0 */ + case 5:/* a1 */ + case 6:/* a2 */ + case 7:/* a3 */ +#if defined(__mips_n64) || defined(__mips_n32) + case 8:/* a4 */ + case 9:/* a5 */ + case 10:/* a6 */ + case 11:/* a7 */ +#endif + arg = i.IType.rt - 4; + if (args) + args[arg] = kdbpeek((int *)(*sp + (short)i.IType.imm)); + if (valid_args) + valid_args[arg] = 1; + case 31: /* ra */ + ra = kdbpeek((int *)(*sp + (short)i.IType.imm)); + } + break; + + case OP_SD: + /* look for saved registers on the stack */ + if (i.IType.rs != 29) + break; + /* only restore the first one */ + if (mask & (1 << i.IType.rt)) + break; + mask |= (1 << i.IType.rt); + switch (i.IType.rt) { + case 4:/* a0 */ + case 5:/* a1 */ + case 6:/* a2 */ + case 7:/* a3 */ +#if defined(__mips_n64) || defined(__mips_n32) + case 8:/* a4 */ + case 9:/* a5 */ + case 10:/* a6 */ + case 11:/* a7 */ +#endif + arg = i.IType.rt - 4; + if (args) + args[arg] = kdbpeekd((int *)(*sp + (short)i.IType.imm)); + if (valid_args) + valid_args[arg] = 1; + break; + + case 31: /* ra */ + ra = kdbpeekd((int *)(*sp + (short)i.IType.imm)); + } + break; + + case OP_ADDI: + case OP_ADDIU: + case OP_DADDI: + case OP_DADDIU: + /* look for stack pointer adjustment */ + if (i.IType.rs != 29 || i.IType.rt != 29) + break; + stksize = -((short)i.IType.imm); + } + } + + if (!MIPS_IS_VALID_KERNELADDR(ra)) + return (-1); + + *pc = ra; + *sp += stksize; + + return (0); +error: + return (-1); +} + +static int +dtrace_next_uframe(register_t *pc, register_t *sp, register_t *ra) +{ + int offset, registers_on_stack; + uint32_t opcode, mask; + register_t function_start; + int stksize; + InstFmt i; + + registers_on_stack = 0; + mask = 0; + function_start = 0; + offset = 0; + stksize = 0; + + while (offset < MAX_FUNCTION_SIZE) { + opcode = dtrace_fuword32((void *)(*pc - offset)); + + /* [d]addiu sp, sp, -X*/ + if (((opcode & 0xffff8000) == 0x27bd8000) + || ((opcode & 0xffff8000) == 0x67bd8000)) { + function_start = *pc - offset; + registers_on_stack = 1; + break; + } + + /* lui gp, X */ + if ((opcode & 0xffff8000) == 0x3c1c0000) { + /* + * Function might start with this instruction + * Keep an eye on "jr ra" and sp correction + * with positive value further on + */ + function_start = *pc - offset; + } + + if (function_start) { + /* + * Stop looking further. Possible end of + * function instruction: it means there is no + * stack modifications, sp is unchanged + */ + + /* [d]addiu sp,sp,X */ + if (((opcode & 0xffff8000) == 0x27bd0000) + || ((opcode & 0xffff8000) == 0x67bd0000)) + break; + + if (opcode == 0x03e00008) + break; + } + + offset += sizeof(int); + } + + if (!function_start) + return (-1); + + if (registers_on_stack) { + offset = 0; + while ((offset < MAX_PROLOGUE_SIZE) + && ((function_start + offset) < *pc)) { + i.word = + dtrace_fuword32((void *)(function_start + offset)); + switch (i.JType.op) { + case OP_SW: + /* look for saved registers on the stack */ + if (i.IType.rs != 29) + break; + /* only restore the first one */ + if (mask & (1 << i.IType.rt)) + break; + mask |= (1 << i.IType.rt); + if (i.IType.rt == 31) + *ra = dtrace_fuword32((void *)(*sp + (short)i.IType.imm)); + break; + + case OP_SD: + /* look for saved registers on the stack */ + if (i.IType.rs != 29) + break; + /* only restore the first one */ + if (mask & (1 << i.IType.rt)) + break; + mask |= (1 << i.IType.rt); + /* ra */ + if (i.IType.rt == 31) + *ra = dtrace_fuword64((void *)(*sp + (short)i.IType.imm)); + break; + + case OP_ADDI: + case OP_ADDIU: + case OP_DADDI: + case OP_DADDIU: + /* look for stack pointer adjustment */ + if (i.IType.rs != 29 || i.IType.rt != 29) + break; + stksize = -((short)i.IType.imm); + } + + offset += sizeof(int); + } + } + + /* + * We reached the end of backtrace + */ + if (*pc == *ra) + return (-1); + + *pc = *ra; + *sp += stksize; + + return (0); +} + +static int +dtrace_copycheck(uintptr_t uaddr, uintptr_t kaddr, size_t size) +{ + + if (uaddr + size > VM_MAXUSER_ADDRESS || uaddr + size < uaddr) { + DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); + cpu_core[curcpu].cpuc_dtrace_illval = uaddr; + return (0); + } + + return (1); +} + +void +dtrace_copyin(uintptr_t uaddr, uintptr_t kaddr, size_t size, + volatile uint16_t *flags) +{ + if (dtrace_copycheck(uaddr, kaddr, size)) + dtrace_copy(uaddr, kaddr, size); +} + +void +dtrace_copyout(uintptr_t kaddr, uintptr_t uaddr, size_t size, + volatile uint16_t *flags) +{ + if (dtrace_copycheck(uaddr, kaddr, size)) + dtrace_copy(kaddr, uaddr, size); +} + +void +dtrace_copyinstr(uintptr_t uaddr, uintptr_t kaddr, size_t size, + volatile uint16_t *flags) +{ + if (dtrace_copycheck(uaddr, kaddr, size)) + dtrace_copystr(uaddr, kaddr, size, flags); +} + +void +dtrace_copyoutstr(uintptr_t kaddr, uintptr_t uaddr, size_t size, + volatile uint16_t *flags) +{ + if (dtrace_copycheck(uaddr, kaddr, size)) + dtrace_copystr(kaddr, uaddr, size, flags); +} + +uint8_t +dtrace_fuword8(void *uaddr) +{ + if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) { + DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); + cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr; + return (0); + } + return (dtrace_fuword8_nocheck(uaddr)); +} + +uint16_t +dtrace_fuword16(void *uaddr) +{ + if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) { + DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); + cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr; + return (0); + } + return (dtrace_fuword16_nocheck(uaddr)); +} + +uint32_t +dtrace_fuword32(void *uaddr) +{ + if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) { + DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); + cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr; + return (0); + } + return (dtrace_fuword32_nocheck(uaddr)); +} + +uint64_t +dtrace_fuword64(void *uaddr) +{ + if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) { + DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); + cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr; + return (0); + } + return (dtrace_fuword64_nocheck(uaddr)); +} --- /dev/null 2012-02-29 23:00:00.000000000 -0800 +++ sys/cddl/dev/dtrace/mips/dtrace_subr.c 2012-02-29 23:06:16.000000000 -0800 @@ -0,0 +1,201 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License, Version 1.0 only + * (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 + * or http://www.opensolaris.org/os/licensing. + * 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 + * + * $FreeBSD$ + * + */ +/* + * Copyright 2005 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DELAYBRANCH(x) ((int)(x) < 0) + +extern uintptr_t dtrace_in_probe_addr; +extern int dtrace_in_probe; +extern dtrace_id_t dtrace_probeid_error; + +int dtrace_invop(uintptr_t, uintptr_t *, uintptr_t); + +typedef struct dtrace_invop_hdlr { + int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t); + struct dtrace_invop_hdlr *dtih_next; +} dtrace_invop_hdlr_t; + +dtrace_invop_hdlr_t *dtrace_invop_hdlr; + +int +dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax) +{ + dtrace_invop_hdlr_t *hdlr; + int rval; + + for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) + if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0) + return (rval); + + return (0); +} + + +/*ARGSUSED*/ +void +dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) +{ + printf("IMPLEMENT ME: dtrace_toxic_ranges\n"); +} + +void +dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) +{ + cpuset_t cpus; + + if (cpu == DTRACE_CPUALL) + cpus = all_cpus; + else + CPU_SETOF(cpu, &cpus); + + smp_rendezvous_cpus(cpus, smp_no_rendevous_barrier, func, + smp_no_rendevous_barrier, arg); +} + +static void +dtrace_sync_func(void) +{ +} + +void +dtrace_sync(void) +{ + dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); +} + +/* + * DTrace needs a high resolution time function which can + * be called from a probe context and guaranteed not to have + * instrumented with probes itself. + * + * Returns nanoseconds since boot. + */ +uint64_t +dtrace_gethrtime() +{ + struct timespec curtime; + + nanouptime(&curtime); + + return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec); + +} + +uint64_t +dtrace_gethrestime(void) +{ + struct timespec curtime; + + getnanotime(&curtime); + + return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec); +} + +/* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */ +int +dtrace_trap(struct trapframe *frame, u_int type) +{ + /* + * A trap can occur while DTrace executes a probe. Before + * executing the probe, DTrace blocks re-scheduling and sets + * a flag in it's per-cpu flags to indicate that it doesn't + * want to fault. On returning from the probe, the no-fault + * flag is cleared and finally re-scheduling is enabled. + * + * Check if DTrace has enabled 'no-fault' mode: + * + */ + if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) { + /* + * There are only a couple of trap types that are expected. + * All the rest will be handled in the usual way. + */ + switch (type) { + /* Page fault. */ + case T_TLB_ST_MISS: + case T_ADDR_ERR_ST: + case T_TLB_LD_MISS: + case T_ADDR_ERR_LD: + case T_BUS_ERR_IFETCH: + /* Flag a bad address. */ + cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; + cpu_core[curcpu].cpuc_dtrace_illval = frame->badvaddr; + + /* + * Offset the instruction pointer to the instruction + * following the one causing the fault. + */ + if (DELAYBRANCH(frame->cause)) /* Check BD bit */ + { + /* XXX: check MipsEmulateBranch on MIPS64 + frame->pc = MipsEmulateBranch(frame, frame->pc, + 0, 0); + */ + panic("%s: delay slot at %p, badvaddr = %p\n", + __func__, + (void*)frame->pc, (void*)frame->badvaddr); + } + else + frame->pc += sizeof(int); + return (1); + default: + /* Handle all other traps in the usual way. */ + break; + } + } + + /* Handle the trap in the usual way. */ + return (0); +} + +void +dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which, + int fault, int fltoffs, uintptr_t illval) +{ + + dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state, + (uintptr_t)epid, + (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs); +} --- /dev/null 2012-02-29 23:00:00.000000000 -0800 +++ sys/cddl/dev/dtrace/mips/regset.h 2012-02-29 23:06:16.000000000 -0800 @@ -0,0 +1,62 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License, Version 1.0 only + * (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 + * or http://www.opensolaris.org/os/licensing. + * 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 + * + * $FreeBSD$ + */ +/* + * Copyright 2004 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 AT&T */ +/* All Rights Reserved */ + +#ifndef _REGSET_H +#define _REGSET_H + +/* + * #pragma ident "@(#)regset.h 1.11 05/06/08 SMI" + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * XXX: define registers properly + */ + +#if 0 +#define REG_PC PC +#define REG_FP EBP +#define REG_SP SP +#define REG_PS EFL +#define REG_R0 EAX +#define REG_R1 EDX +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* _REGSET_H */ --- sys/cddl/dev/lockstat/lockstat.c.orig +++ sys/cddl/dev/lockstat/lockstat.c @@ -45,7 +45,7 @@ #include #include -#if defined(__i386__) || defined(__amd64__) +#if defined(__i386__) || defined(__amd64__) || defined(__mips__) #define LOCKSTAT_AFRAMES 1 #else #error "architecture not supported" --- sys/cddl/dev/profile/profile.c.orig +++ sys/cddl/dev/profile/profile.c @@ -112,6 +112,10 @@ #endif #endif +#ifdef __mips +#define PROF_ARTIFICIAL_FRAMES 3 +#endif + typedef struct profile_probe { char prof_name[PROF_NAMELEN]; dtrace_id_t prof_id; --- sys/mips/include/db_machdep.h.orig +++ sys/mips/include/db_machdep.h @@ -95,5 +95,6 @@ void stacktrace_subr(register_t pc, register_t sp, register_t ra, int (*)(const char *, ...)); int32_t kdbpeek(int *); int64_t kdbpeekd(int *); +int db_trace_location(register_t pc, register_t sp, register_t ra); #endif /* !_MIPS_DB_MACHDEP_H_ */ --- sys/mips/include/trap.h.orig +++ sys/mips/include/trap.h @@ -117,6 +117,7 @@ void MipsUserIntr(void); register_t trap(struct trapframe *); +void kernel_stack_overflow(struct trapframe *frame); #ifndef LOCORE /* XXX */ int check_address(void *); --- sys/mips/mips/db_trace.c.orig +++ sys/mips/mips/db_trace.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -49,14 +50,6 @@ */ #define MIPS_END_OF_FUNCTION(ins) ((ins) == 0x03e00008) -#if defined(__mips_n64) -# define MIPS_IS_VALID_KERNELADDR(reg) ((((reg) & 3) == 0) && \ - ((vm_offset_t)(reg) >= MIPS_XKPHYS_START)) -#else -# define MIPS_IS_VALID_KERNELADDR(reg) ((((reg) & 3) == 0) && \ - ((vm_offset_t)(reg) >= MIPS_KSEG0_START)) -#endif - /* * Functions ``special'' enough to print by name */ @@ -434,6 +427,15 @@ return (0); } +int +db_trace_location(register_t pc, register_t sp, register_t ra) +{ + stacktrace_subr(pc, sp, ra, + (int (*) (const char *, ...))db_printf); + + return (0); +} + void db_show_mdpcpu(struct pcpu *pc) { --- sys/mips/mips/exception.S.orig +++ sys/mips/mips/exception.S @@ -63,11 +63,33 @@ #include #include "opt_cputype.h" +#include "opt_kdtrace.h" #include "assym.s" .set noreorder # Noreorder is default style! +#ifdef KDTRACE_HOOKS +/* XXX: mips64 */ + .data + .globl dtrace_invop_jump_addr + .align 4 + .type dtrace_invop_jump_addr, @object + .size dtrace_invop_jump_addr, 8 +dtrace_invop_jump_addr: + .word 0 + .word 0 + .globl dtrace_invop_calltrap_addr + .align 4 + .type dtrace_invop_calltrap_addr, @object + .size dtrace_invop_calltrap_addr, 8 +dtrace_invop_calltrap_addr: + .word 0 + .word 0 +#endif + + .text + /* * Reasonable limit */ @@ -979,16 +1001,18 @@ * the trapframe. */ SAVE_REG(k1, SP, sp) + move a3, k1 /* * Squelch any more overflow checks by setting the stack base to 0. */ GET_CPU_PCPU(k1) PTR_L k0, PC_CURTHREAD(k1) + PTR_L a2, TD_KSTACK(k0) PTR_S zero, TD_KSTACK(k0) move a1, a0 - PANIC("kernel stack overflow - trapframe at %p") + jal kernel_stack_overflow /* * This nop is necessary so that the 'ra' remains within the bounds --- sys/mips/mips/trap.c.orig +++ sys/mips/mips/trap.c @@ -44,6 +44,7 @@ #include "opt_ddb.h" #include "opt_global.h" #include "opt_ktrace.h" +#include "opt_kdtrace.h" #define NO_REG_DEFS 1 /* Prevent asm.h from including regdef.h */ #include @@ -91,6 +92,33 @@ #include #endif +#ifdef KDTRACE_HOOKS +#include + +/* + * This is a hook which is initialised by the dtrace module + * to handle traps which might occur during DTrace probe + * execution. + */ +dtrace_trap_func_t dtrace_trap_func; + +dtrace_doubletrap_func_t dtrace_doubletrap_func; + +/* + * This is a hook which is initialised by the systrace module + * when it is loaded. This keeps the DTrace syscall provider + * implementation opaque. + */ +systrace_probe_func_t systrace_probe_func; + +/* + * These hooks are necessary for the pid, usdt and fasttrap providers. + */ +dtrace_fasttrap_probe_ptr_t dtrace_fasttrap_probe_ptr; +dtrace_pid_probe_ptr_t dtrace_pid_probe_ptr; +dtrace_return_probe_ptr_t dtrace_return_probe_ptr; +#endif + #ifdef TRAP_DEBUG int trap_debug = 0; SYSCTL_INT(_machdep, OID_AUTO, trap_debug, CTLFLAG_RW, @@ -489,6 +517,46 @@ } } #endif + +#ifdef KDTRACE_HOOKS + /* + * A trap can occur while DTrace executes a probe. Before + * executing the probe, DTrace blocks re-scheduling and sets + * a flag in it's per-cpu flags to indicate that it doesn't + * want to fault. On returning from the probe, the no-fault + * flag is cleared and finally re-scheduling is enabled. + * + * If the DTrace kernel module has registered a trap handler, + * call it and if it returns non-zero, assume that it has + * handled the trap and modified the trap frame so that this + * function can return normally. + */ +#if 0 + if (type == T_DTRACE_PROBE || type == T_DTRACE_RET || + type == T_BPTFLT) { + struct reg regs; + + fill_frame_regs(frame, ®s); + if (type == T_DTRACE_PROBE && + dtrace_fasttrap_probe_ptr != NULL && + dtrace_fasttrap_probe_ptr(®s) == 0) + goto out; + else if (type == T_BPTFLT && + dtrace_pid_probe_ptr != NULL && + dtrace_pid_probe_ptr(®s) == 0) + goto out; + else if (type == T_DTRACE_RET && + dtrace_return_probe_ptr != NULL && + dtrace_return_probe_ptr(®s) == 0) + goto out; + } +#endif + if (!usermode) { + if (dtrace_trap_func != NULL && (*dtrace_trap_func)(trapframe, type)) + return (trapframe->pc); + } +#endif + switch (type) { case T_MCHECK: #ifdef DDB @@ -593,6 +661,9 @@ PROC_LOCK(p); --p->p_lock; PROC_UNLOCK(p); + /* + * XXXDTRACE: add dtrace_doubletrap_func here? + */ #ifdef VMFAULT_TRACE printf("vm_fault(%p (pmap %p), %p (%p), %x, %d) -> %x at pc %p\n", map, &vm->vm_pmap, (void *)va, (void *)(intptr_t)trapframe->badvaddr, @@ -1539,3 +1610,13 @@ } return access_type; } + +void +kernel_stack_overflow(struct trapframe *frame) +{ + printf("pc = %#jx\n", (intmax_t)frame->pc); + printf("ra = %#jx\n", (intmax_t)frame->ra); + printf("badvaddr = %#jx\n", (intmax_t)frame->badvaddr); + db_trace_location(frame->pc, frame->sp, frame->ra); + panic("kernel stack overflow"); +} --- sys/modules/cyclic/Makefile.orig +++ sys/modules/cyclic/Makefile @@ -10,7 +10,7 @@ CFLAGS+= -I${.CURDIR}/../../cddl/compat/opensolaris \ -I${.CURDIR}/../../cddl/contrib/opensolaris/uts/common \ -I${.CURDIR}/../.. \ - -I${.CURDIR}/../../cddl/dev/cyclic/${MACHINE_CPUARCH:S/amd64/i386/} + -I${.CURDIR}/../../cddl/dev/cyclic/i386 CFLAGS+= -DDEBUG=1 --- sys/modules/dtrace/Makefile.orig +++ sys/modules/dtrace/Makefile @@ -9,14 +9,12 @@ dtrace \ dtraceall \ dtrace_test \ - lockstat \ - profile \ prototype \ sdt \ systrace .if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386" -SUBDIR+= fasttrap fbt systrace_linux32 +SUBDIR+= fasttrap fbt lockstat profile systrace_linux32 .endif .if ${MACHINE_CPUARCH} == "amd64" SUBDIR+= systrace_freebsd32 --- sys/sys/elf_common.h.orig +++ sys/sys/elf_common.h @@ -296,6 +296,7 @@ #define SHT_HIOS 0x6fffffff /* Last of OS specific semantics */ #define SHT_LOPROC 0x70000000 /* reserved range for processor */ #define SHT_AMD64_UNWIND 0x70000001 /* unwind information */ +#define SHT_MIPS_OPTIONS 0x7000000d #define SHT_MIPS_DWARF 0x7000001e /* MIPS gcc uses MIPS_DWARF */ #define SHT_HIPROC 0x7fffffff /* specific section header types */ #define SHT_LOUSER 0x80000000 /* reserved range for application */