1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21/* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26#pragma ident "%Z%%M% %I% %E% SMI" 27 28/* 29 * SFMMU primitives. These primitives should only be used by sfmmu 30 * routines. 31 */ 32 33#if defined(lint) 34#include <sys/types.h> 35#else /* lint */ 36#include "assym.h" 37#endif /* lint */ 38 39#include <sys/asm_linkage.h> 40#include <sys/machtrap.h> 41#include <sys/machasi.h> 42#include <sys/sun4asi.h> 43#include <sys/pte.h> 44#include <sys/mmu.h> 45#include <vm/hat_sfmmu.h> 46#include <vm/seg_spt.h> 47#include <sys/machparam.h> 48#include <sys/privregs.h> 49#include <sys/scb.h> 50#include <sys/intreg.h> 51#include <sys/machthread.h> 52#include <sys/clock.h> 53#include <sys/trapstat.h> 54 55/* 56 * sfmmu related subroutines 57 */ 58 59#if defined (lint) 60 61/* ARGSUSED */ 62void 63sfmmu_raise_tsb_exception(uint64_t sfmmup, uint64_t rctx) 64{} 65 66int 67sfmmu_getctx_pri() 68{ return(0); } 69 70int 71sfmmu_getctx_sec() 72{ return(0); } 73 74/* ARGSUSED */ 75void 76sfmmu_setctx_sec(int ctx) 77{} 78 79/* ARGSUSED */ 80void 81sfmmu_load_mmustate(sfmmu_t *sfmmup) 82{ 83} 84 85#else /* lint */ 86 87/* 88 * Invalidate either the context of a specific victim or any process 89 * currently running on this CPU. 90 * 91 * %g1 = sfmmup whose ctx is being stolen (victim) 92 * when called from sfmmu_wrap_around, %g1 == INVALID_CONTEXT. 93 * Note %g1 is the only input argument used by this xcall handler. 94 */ 95 96 ENTRY(sfmmu_raise_tsb_exception) 97 ! 98 ! if (victim == INVALID_CONTEXT) { 99 ! if (sec-ctx > INVALID_CONTEXT) 100 ! write INVALID_CONTEXT to sec-ctx 101 ! if (pri-ctx > INVALID_CONTEXT) 102 ! write INVALID_CONTEXT to pri-ctx 103 ! 104 ! } else if (current CPU tsbmiss->usfmmup != victim sfmmup) { 105 ! return 106 ! } else { 107 ! if (sec-ctx > INVALID_CONTEXT) 108 ! write INVALID_CONTEXT to sec-ctx 109 ! 110 ! if (pri-ctx > INVALID_CONTEXT) 111 ! write INVALID_CONTEXT to pri-ctx 112 ! } 113 ! 114 115 sethi %hi(ksfmmup), %g3 116 ldx [%g3 + %lo(ksfmmup)], %g3 117 cmp %g1, %g3 118 be,a,pn %xcc, ptl1_panic /* can't invalidate kernel ctx */ 119 mov PTL1_BAD_RAISE_TSBEXCP, %g1 120 121 set INVALID_CONTEXT, %g2 122 123 cmp %g1, INVALID_CONTEXT 124 bne,pt %xcc, 1f /* called from wrap_around? */ 125 mov MMU_SCONTEXT, %g3 126 127 ldxa [%g3]ASI_MMU_CTX, %g5 /* %g5 = sec-ctx */ 128 cmp %g5, INVALID_CONTEXT /* kernel or invalid ctx ? */ 129 ble,pn %xcc, 0f /* yes, no need to change */ 130 mov MMU_PCONTEXT, %g7 131 132 stxa %g2, [%g3]ASI_MMU_CTX /* set invalid ctx */ 133 membar #Sync 134 1350: 136 ldxa [%g7]ASI_MMU_CTX, %g5 /* %g5 = pri-ctx */ 137 cmp %g5, INVALID_CONTEXT /* kernel or invalid ctx? */ 138 ble,pn %xcc, 6f /* yes, no need to change */ 139 nop 140 141 stxa %g2, [%g7]ASI_MMU_CTX /* set pri-ctx to invalid */ 142 membar #Sync 143 1446: /* flushall tlb */ 145 mov %o0, %g3 146 mov %o1, %g4 147 mov %o2, %g6 148 mov %o5, %g7 149 150 mov %g0, %o0 ! XXX no cpu list yet 151 mov %g0, %o1 ! XXX no cpu list yet 152 mov MAP_ITLB | MAP_DTLB, %o2 153 mov MMU_DEMAP_ALL, %o5 154 ta FAST_TRAP 155 brz,pt %o0, 5f 156 nop 157 ba panic_bad_hcall 158 mov MMU_DEMAP_ALL, %o1 1595: 160 mov %g3, %o0 161 mov %g4, %o1 162 mov %g6, %o2 163 mov %g7, %o5 164 165 ba 3f 166 nop 1671: 168 /* 169 * %g1 = sfmmup 170 * %g2 = INVALID_CONTEXT 171 * %g3 = MMU_SCONTEXT 172 */ 173 CPU_TSBMISS_AREA(%g5, %g6) /* load cpu tsbmiss area */ 174 ldx [%g5 + TSBMISS_UHATID], %g5 /* load usfmmup */ 175 176 cmp %g5, %g1 /* is it the victim? */ 177 bne,pt %xcc, 2f /* is our sec-ctx a victim? */ 178 nop 179 180 ldxa [%g3]ASI_MMU_CTX, %g5 /* %g5 = sec-ctx */ 181 cmp %g5, INVALID_CONTEXT /* kernel or invalid ctx ? */ 182 ble,pn %xcc, 0f /* yes, no need to change */ 183 mov MMU_PCONTEXT, %g7 184 185 stxa %g2, [%g3]ASI_MMU_CTX /* set sec-ctx to invalid */ 186 membar #Sync 187 1880: 189 ldxa [%g7]ASI_MMU_CTX, %g4 /* %g4 = pri-ctx */ 190 cmp %g4, INVALID_CONTEXT /* is pri-ctx the victim? */ 191 ble %icc, 3f /* no need to change pri-ctx */ 192 nop 193 stxa %g2, [%g7]ASI_MMU_CTX /* set pri-ctx to invalid */ 194 membar #Sync 195 1963: 197 /* TSB program must be cleared - walkers do not check a context. */ 198 mov %o0, %g3 199 mov %o1, %g4 200 mov %o5, %g7 201 clr %o0 202 clr %o1 203 mov MMU_TSB_CTXNON0, %o5 204 ta FAST_TRAP 205 brnz,a,pn %o0, ptl1_panic 206 mov PTL1_BAD_HCALL, %g1 207 mov %g3, %o0 208 mov %g4, %o1 209 mov %g7, %o5 2102: 211 retry 212 SET_SIZE(sfmmu_raise_tsb_exception) 213 214 ENTRY_NP(sfmmu_getctx_pri) 215 set MMU_PCONTEXT, %o0 216 retl 217 ldxa [%o0]ASI_MMU_CTX, %o0 218 SET_SIZE(sfmmu_getctx_pri) 219 220 ENTRY_NP(sfmmu_getctx_sec) 221 set MMU_SCONTEXT, %o0 222 retl 223 ldxa [%o0]ASI_MMU_CTX, %o0 224 SET_SIZE(sfmmu_getctx_sec) 225 226 /* 227 * Set the secondary context register for this process. 228 * %o0 = context number 229 */ 230 ENTRY_NP(sfmmu_setctx_sec) 231 /* 232 * From resume we call sfmmu_setctx_sec with interrupts disabled. 233 * But we can also get called from C with interrupts enabled. So, 234 * we need to check first. 235 */ 236 237 /* If interrupts are not disabled, then disable them */ 238 rdpr %pstate, %g1 239 btst PSTATE_IE, %g1 240 bnz,a,pt %icc, 1f 241 wrpr %g1, PSTATE_IE, %pstate /* disable interrupts */ 2421: 243 mov MMU_SCONTEXT, %o1 244 sethi %hi(FLUSH_ADDR), %o4 245 stxa %o0, [%o1]ASI_MMU_CTX /* set 2nd context reg. */ 246 flush %o4 247 248 /* 249 * if the routine is entered with intr enabled, then enable intr now. 250 * otherwise, keep intr disabled, return without enabing intr. 251 * %g1 - old intr state 252 */ 253 btst PSTATE_IE, %g1 254 bnz,a,pt %icc, 2f 255 wrpr %g0, %g1, %pstate /* enable interrupts */ 2562: retl 257 nop 258 SET_SIZE(sfmmu_setctx_sec) 259 260 /* 261 * set ktsb_phys to 1 if the processor supports ASI_QUAD_LDD_PHYS. 262 * returns the detection value in %o0. 263 */ 264 ENTRY_NP(sfmmu_setup_4lp) 265 set ktsb_phys, %o2 266 mov 1, %o1 267 st %o1, [%o2] 268 retl 269 mov %o1, %o0 270 SET_SIZE(sfmmu_setup_4lp) 271 272 /* 273 * Called to load MMU registers and tsbmiss area 274 * for the active process. This function should 275 * only be called from TL=0. 276 * 277 * %o0 - hat pointer 278 */ 279 ENTRY_NP(sfmmu_load_mmustate) 280 281#ifdef DEBUG 282 PANIC_IF_INTR_ENABLED_PSTR(msfmmu_ei_l1, %g1) 283#endif /* DEBUG */ 284 285 sethi %hi(ksfmmup), %o3 286 ldx [%o3 + %lo(ksfmmup)], %o3 287 cmp %o3, %o0 288 be,pn %xcc, 3f ! if kernel as, do nothing 289 nop 290 291 /* 292 * We need to set up the TSB base register, tsbmiss 293 * area, and pass the TSB information into the hypervisor 294 */ 295 ldx [%o0 + SFMMU_TSB], %o1 ! %o1 = first tsbinfo 296 ldx [%o1 + TSBINFO_NEXTPTR], %g2 ! %g2 = second tsbinfo 297 298 /* create/set first UTSBREG */ 299 MAKE_UTSBREG(%o1, %o2, %o3) ! %o2 = user tsbreg 300 SET_UTSBREG(SCRATCHPAD_UTSBREG1, %o2, %o3) 301 302 brz,pt %g2, 2f 303 mov -1, %o2 ! use -1 if no second TSB 304 305 /* make 2nd UTSBREG */ 306 MAKE_UTSBREG(%g2, %o2, %o3) ! %o2 = user tsbreg 3072: 308 SET_UTSBREG(SCRATCHPAD_UTSBREG2, %o2, %o3) 309 310#ifdef DEBUG 311 /* check if hypervisor/hardware should handle user TSB */ 312 sethi %hi(hv_use_non0_tsb), %o2 313 ld [%o2 + %lo(hv_use_non0_tsb)], %o2 314 brz,pn %o2, 5f 315 nop 316#endif /* DEBUG */ 317 CPU_ADDR(%o2, %o4) ! load CPU struct addr to %o2 using %o4 318 ldub [%o2 + CPU_TSTAT_FLAGS], %o1 ! load cpu_tstat_flag to %o1 319 320 /* 321 * %o0 = sfmmup 322 * %o2 = returned sfmmu cnum on this CPU 323 * %o4 = scratch 324 */ 325 SFMMU_CPU_CNUM(%o0, %o2, %o4) 326 327 mov %o5, %o4 ! preserve %o5 for resume 328 mov %o0, %o3 ! preserve %o0 329 btst TSTAT_TLB_STATS, %o1 330 bnz,a,pn %icc, 4f ! ntsb = 0 if TLB stats enabled 331 clr %o0 332 cmp %o2, INVALID_CONTEXT 333 be,a,pn %icc, 4f 334 clr %o0 ! ntsb = 0 for invalid ctx 335 ldx [%o3 + SFMMU_HVBLOCK + HV_TSB_INFO_CNT], %o0 3364: 337 ldx [%o3 + SFMMU_HVBLOCK + HV_TSB_INFO_PA], %o1 338 mov MMU_TSB_CTXNON0, %o5 339 ta FAST_TRAP ! set TSB info for user process 340 brnz,a,pn %o0, panic_bad_hcall 341 mov MMU_TSB_CTXNON0, %o1 342 mov %o3, %o0 ! restore %o0 343 mov %o4, %o5 ! restore %o5 3445: 345 ldx [%o0 + SFMMU_ISMBLKPA], %o1 ! copy members of sfmmu 346 CPU_TSBMISS_AREA(%o2, %o3) ! we need to access from 347 stx %o1, [%o2 + TSBMISS_ISMBLKPA] ! sfmmu_tsb_miss into the 348 lduh [%o0 + SFMMU_FLAGS], %o3 ! per-CPU tsbmiss area. 349 stx %o0, [%o2 + TSBMISS_UHATID] 350 stuh %o3, [%o2 + TSBMISS_HATFLAGS] 351 3523: retl 353 nop 354 SET_SIZE(sfmmu_load_mmustate) 355 356#endif /* lint */ 357 358#if defined(lint) 359 360/* Prefetch "struct tsbe" while walking TSBs */ 361/*ARGSUSED*/ 362void 363prefetch_tsbe_read(struct tsbe *tsbep) 364{} 365 366/* Prefetch the tsbe that we are about to write */ 367/*ARGSUSED*/ 368void 369prefetch_tsbe_write(struct tsbe *tsbep) 370{} 371 372#else /* lint */ 373 374 ENTRY(prefetch_tsbe_read) 375 retl 376 nop 377 SET_SIZE(prefetch_tsbe_read) 378 379 ENTRY(prefetch_tsbe_write) 380 retl 381 nop 382 SET_SIZE(prefetch_tsbe_write) 383#endif /* lint */ 384