HardenedBSD src tree https://hardenedbsd.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

720 lines
18 KiB

  1. /*-
  2. * SPDX-License-Identifier: BSD-4-Clause
  3. *
  4. * Copyright (c) 1982, 1986 The Regents of the University of California.
  5. * Copyright (c) 1989, 1990 William Jolitz
  6. * Copyright (c) 1994 John Dyson
  7. * All rights reserved.
  8. *
  9. * This code is derived from software contributed to Berkeley by
  10. * the Systems Programming Group of the University of Utah Computer
  11. * Science Department, and William Jolitz.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. All advertising materials mentioning features or use of this software
  22. * must display the following acknowledgement:
  23. * This product includes software developed by the University of
  24. * California, Berkeley and its contributors.
  25. * 4. Neither the name of the University nor the names of its contributors
  26. * may be used to endorse or promote products derived from this software
  27. * without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  30. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  37. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  38. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  39. * SUCH DAMAGE.
  40. *
  41. * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
  42. * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
  43. */
  44. #include <sys/cdefs.h>
  45. __FBSDID("$FreeBSD$");
  46. #include "opt_isa.h"
  47. #include "opt_npx.h"
  48. #include "opt_reset.h"
  49. #include "opt_cpu.h"
  50. #include <sys/param.h>
  51. #include <sys/systm.h>
  52. #include <sys/bio.h>
  53. #include <sys/buf.h>
  54. #include <sys/kernel.h>
  55. #include <sys/ktr.h>
  56. #include <sys/lock.h>
  57. #include <sys/malloc.h>
  58. #include <sys/mbuf.h>
  59. #include <sys/mutex.h>
  60. #include <sys/pioctl.h>
  61. #include <sys/proc.h>
  62. #include <sys/sysent.h>
  63. #include <sys/sf_buf.h>
  64. #include <sys/smp.h>
  65. #include <sys/sched.h>
  66. #include <sys/sysctl.h>
  67. #include <sys/unistd.h>
  68. #include <sys/vnode.h>
  69. #include <sys/vmmeter.h>
  70. #include <machine/cpu.h>
  71. #include <machine/cputypes.h>
  72. #include <machine/md_var.h>
  73. #include <machine/pcb.h>
  74. #include <machine/pcb_ext.h>
  75. #include <machine/smp.h>
  76. #include <machine/vm86.h>
  77. #include <vm/vm.h>
  78. #include <vm/vm_extern.h>
  79. #include <vm/vm_kern.h>
  80. #include <vm/vm_page.h>
  81. #include <vm/vm_map.h>
  82. #include <vm/vm_param.h>
  83. #ifndef NSFBUFS
  84. #define NSFBUFS (512 + maxusers * 16)
  85. #endif
  86. _Static_assert(__OFFSETOF_MONITORBUF == offsetof(struct pcpu, pc_monitorbuf),
  87. "__OFFSETOF_MONITORBUF does not correspond with offset of pc_monitorbuf.");
  88. union savefpu *
  89. get_pcb_user_save_td(struct thread *td)
  90. {
  91. vm_offset_t p;
  92. p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
  93. roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN);
  94. KASSERT((p % XSAVE_AREA_ALIGN) == 0, ("Unaligned pcb_user_save area"));
  95. return ((union savefpu *)p);
  96. }
  97. union savefpu *
  98. get_pcb_user_save_pcb(struct pcb *pcb)
  99. {
  100. vm_offset_t p;
  101. p = (vm_offset_t)(pcb + 1);
  102. return ((union savefpu *)p);
  103. }
  104. struct pcb *
  105. get_pcb_td(struct thread *td)
  106. {
  107. vm_offset_t p;
  108. p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
  109. roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN) -
  110. sizeof(struct pcb);
  111. return ((struct pcb *)p);
  112. }
  113. void *
  114. alloc_fpusave(int flags)
  115. {
  116. void *res;
  117. struct savefpu_ymm *sf;
  118. res = malloc(cpu_max_ext_state_size, M_DEVBUF, flags);
  119. if (use_xsave) {
  120. sf = (struct savefpu_ymm *)res;
  121. bzero(&sf->sv_xstate.sx_hd, sizeof(sf->sv_xstate.sx_hd));
  122. sf->sv_xstate.sx_hd.xstate_bv = xsave_mask;
  123. }
  124. return (res);
  125. }
  126. /*
  127. * Finish a fork operation, with process p2 nearly set up.
  128. * Copy and update the pcb, set up the stack so that the child
  129. * ready to run and return to user mode.
  130. */
  131. void
  132. cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
  133. {
  134. struct proc *p1;
  135. struct pcb *pcb2;
  136. struct mdproc *mdp2;
  137. p1 = td1->td_proc;
  138. if ((flags & RFPROC) == 0) {
  139. if ((flags & RFMEM) == 0) {
  140. /* unshare user LDT */
  141. struct mdproc *mdp1 = &p1->p_md;
  142. struct proc_ldt *pldt, *pldt1;
  143. mtx_lock_spin(&dt_lock);
  144. if ((pldt1 = mdp1->md_ldt) != NULL &&
  145. pldt1->ldt_refcnt > 1) {
  146. pldt = user_ldt_alloc(mdp1, pldt1->ldt_len);
  147. if (pldt == NULL)
  148. panic("could not copy LDT");
  149. mdp1->md_ldt = pldt;
  150. set_user_ldt(mdp1);
  151. user_ldt_deref(pldt1);
  152. } else
  153. mtx_unlock_spin(&dt_lock);
  154. }
  155. return;
  156. }
  157. /* Ensure that td1's pcb is up to date. */
  158. if (td1 == curthread)
  159. td1->td_pcb->pcb_gs = rgs();
  160. critical_enter();
  161. if (PCPU_GET(fpcurthread) == td1)
  162. npxsave(td1->td_pcb->pcb_save);
  163. critical_exit();
  164. /* Point the pcb to the top of the stack */
  165. pcb2 = get_pcb_td(td2);
  166. td2->td_pcb = pcb2;
  167. /* Copy td1's pcb */
  168. bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
  169. /* Properly initialize pcb_save */
  170. pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
  171. bcopy(get_pcb_user_save_td(td1), get_pcb_user_save_pcb(pcb2),
  172. cpu_max_ext_state_size);
  173. /* Point mdproc and then copy over td1's contents */
  174. mdp2 = &p2->p_md;
  175. bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
  176. /*
  177. * Create a new fresh stack for the new process.
  178. * Copy the trap frame for the return to user mode as if from a
  179. * syscall. This copies most of the user mode register values.
  180. * The -VM86_STACK_SPACE (-16) is so we can expand the trapframe
  181. * if we go to vm86.
  182. */
  183. td2->td_frame = (struct trapframe *)((caddr_t)td2->td_pcb -
  184. VM86_STACK_SPACE) - 1;
  185. bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
  186. td2->td_frame->tf_eax = 0; /* Child returns zero */
  187. td2->td_frame->tf_eflags &= ~PSL_C; /* success */
  188. td2->td_frame->tf_edx = 1;
  189. /*
  190. * If the parent process has the trap bit set (i.e. a debugger had
  191. * single stepped the process to the system call), we need to clear
  192. * the trap flag from the new frame unless the debugger had set PF_FORK
  193. * on the parent. Otherwise, the child will receive a (likely
  194. * unexpected) SIGTRAP when it executes the first instruction after
  195. * returning to userland.
  196. */
  197. if ((p1->p_pfsflags & PF_FORK) == 0)
  198. td2->td_frame->tf_eflags &= ~PSL_T;
  199. /*
  200. * Set registers for trampoline to user mode. Leave space for the
  201. * return address on stack. These are the kernel mode register values.
  202. */
  203. #if defined(PAE) || defined(PAE_TABLES)
  204. pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdpt);
  205. #else
  206. pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdir);
  207. #endif
  208. pcb2->pcb_edi = 0;
  209. pcb2->pcb_esi = (int)fork_return; /* fork_trampoline argument */
  210. pcb2->pcb_ebp = 0;
  211. pcb2->pcb_esp = (int)td2->td_frame - sizeof(void *);
  212. pcb2->pcb_ebx = (int)td2; /* fork_trampoline argument */
  213. pcb2->pcb_eip = (int)fork_trampoline + setidt_disp;
  214. /*-
  215. * pcb2->pcb_dr*: cloned above.
  216. * pcb2->pcb_savefpu: cloned above.
  217. * pcb2->pcb_flags: cloned above.
  218. * pcb2->pcb_onfault: cloned above (always NULL here?).
  219. * pcb2->pcb_gs: cloned above.
  220. * pcb2->pcb_ext: cleared below.
  221. */
  222. /*
  223. * XXX don't copy the i/o pages. this should probably be fixed.
  224. */
  225. pcb2->pcb_ext = 0;
  226. /* Copy the LDT, if necessary. */
  227. mtx_lock_spin(&dt_lock);
  228. if (mdp2->md_ldt != NULL) {
  229. if (flags & RFMEM) {
  230. mdp2->md_ldt->ldt_refcnt++;
  231. } else {
  232. mdp2->md_ldt = user_ldt_alloc(mdp2,
  233. mdp2->md_ldt->ldt_len);
  234. if (mdp2->md_ldt == NULL)
  235. panic("could not copy LDT");
  236. }
  237. }
  238. mtx_unlock_spin(&dt_lock);
  239. /* Setup to release spin count in fork_exit(). */
  240. td2->td_md.md_spinlock_count = 1;
  241. td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
  242. /*
  243. * Now, cpu_switch() can schedule the new process.
  244. * pcb_esp is loaded pointing to the cpu_switch() stack frame
  245. * containing the return address when exiting cpu_switch.
  246. * This will normally be to fork_trampoline(), which will have
  247. * %ebx loaded with the new proc's pointer. fork_trampoline()
  248. * will set up a stack to call fork_return(p, frame); to complete
  249. * the return to user-mode.
  250. */
  251. }
  252. /*
  253. * Intercept the return address from a freshly forked process that has NOT
  254. * been scheduled yet.
  255. *
  256. * This is needed to make kernel threads stay in kernel mode.
  257. */
  258. void
  259. cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
  260. {
  261. /*
  262. * Note that the trap frame follows the args, so the function
  263. * is really called like this: func(arg, frame);
  264. */
  265. td->td_pcb->pcb_esi = (int) func; /* function */
  266. td->td_pcb->pcb_ebx = (int) arg; /* first arg */
  267. }
  268. void
  269. cpu_exit(struct thread *td)
  270. {
  271. /*
  272. * If this process has a custom LDT, release it. Reset pc->pcb_gs
  273. * and %gs before we free it in case they refer to an LDT entry.
  274. */
  275. mtx_lock_spin(&dt_lock);
  276. if (td->td_proc->p_md.md_ldt) {
  277. td->td_pcb->pcb_gs = _udatasel;
  278. load_gs(_udatasel);
  279. user_ldt_free(td);
  280. } else
  281. mtx_unlock_spin(&dt_lock);
  282. }
  283. void
  284. cpu_thread_exit(struct thread *td)
  285. {
  286. critical_enter();
  287. if (td == PCPU_GET(fpcurthread))
  288. npxdrop();
  289. critical_exit();
  290. /* Disable any hardware breakpoints. */
  291. if (td->td_pcb->pcb_flags & PCB_DBREGS) {
  292. reset_dbregs();
  293. td->td_pcb->pcb_flags &= ~PCB_DBREGS;
  294. }
  295. }
  296. void
  297. cpu_thread_clean(struct thread *td)
  298. {
  299. struct pcb *pcb;
  300. pcb = td->td_pcb;
  301. if (pcb->pcb_ext != NULL) {
  302. /* if (pcb->pcb_ext->ext_refcount-- == 1) ?? */
  303. /*
  304. * XXX do we need to move the TSS off the allocated pages
  305. * before freeing them? (not done here)
  306. */
  307. pmap_trm_free(pcb->pcb_ext, ctob(IOPAGES + 1));
  308. pcb->pcb_ext = NULL;
  309. }
  310. }
  311. void
  312. cpu_thread_swapin(struct thread *td)
  313. {
  314. }
  315. void
  316. cpu_thread_swapout(struct thread *td)
  317. {
  318. }
  319. void
  320. cpu_thread_alloc(struct thread *td)
  321. {
  322. struct pcb *pcb;
  323. struct xstate_hdr *xhdr;
  324. td->td_pcb = pcb = get_pcb_td(td);
  325. td->td_frame = (struct trapframe *)((caddr_t)pcb -
  326. VM86_STACK_SPACE) - 1;
  327. pcb->pcb_ext = NULL;
  328. pcb->pcb_save = get_pcb_user_save_pcb(pcb);
  329. if (use_xsave) {
  330. xhdr = (struct xstate_hdr *)(pcb->pcb_save + 1);
  331. bzero(xhdr, sizeof(*xhdr));
  332. xhdr->xstate_bv = xsave_mask;
  333. }
  334. }
  335. void
  336. cpu_thread_free(struct thread *td)
  337. {
  338. cpu_thread_clean(td);
  339. }
  340. bool
  341. cpu_exec_vmspace_reuse(struct proc *p __unused, vm_map_t map __unused)
  342. {
  343. return (true);
  344. }
  345. int
  346. cpu_procctl(struct thread *td __unused, int idtype __unused, id_t id __unused,
  347. int com __unused, void *data __unused)
  348. {
  349. return (EINVAL);
  350. }
  351. void
  352. cpu_set_syscall_retval(struct thread *td, int error)
  353. {
  354. switch (error) {
  355. case 0:
  356. td->td_frame->tf_eax = td->td_retval[0];
  357. td->td_frame->tf_edx = td->td_retval[1];
  358. td->td_frame->tf_eflags &= ~PSL_C;
  359. break;
  360. case ERESTART:
  361. /*
  362. * Reconstruct pc, assuming lcall $X,y is 7 bytes, int
  363. * 0x80 is 2 bytes. We saved this in tf_err.
  364. */
  365. td->td_frame->tf_eip -= td->td_frame->tf_err;
  366. break;
  367. case EJUSTRETURN:
  368. break;
  369. default:
  370. td->td_frame->tf_eax = SV_ABI_ERRNO(td->td_proc, error);
  371. td->td_frame->tf_eflags |= PSL_C;
  372. break;
  373. }
  374. }
  375. /*
  376. * Initialize machine state, mostly pcb and trap frame for a new
  377. * thread, about to return to userspace. Put enough state in the new
  378. * thread's PCB to get it to go back to the fork_return(), which
  379. * finalizes the thread state and handles peculiarities of the first
  380. * return to userspace for the new thread.
  381. */
  382. void
  383. cpu_copy_thread(struct thread *td, struct thread *td0)
  384. {
  385. struct pcb *pcb2;
  386. /* Point the pcb to the top of the stack. */
  387. pcb2 = td->td_pcb;
  388. /*
  389. * Copy the upcall pcb. This loads kernel regs.
  390. * Those not loaded individually below get their default
  391. * values here.
  392. */
  393. bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
  394. pcb2->pcb_flags &= ~(PCB_NPXINITDONE | PCB_NPXUSERINITDONE |
  395. PCB_KERNNPX);
  396. pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
  397. bcopy(get_pcb_user_save_td(td0), pcb2->pcb_save,
  398. cpu_max_ext_state_size);
  399. /*
  400. * Create a new fresh stack for the new thread.
  401. */
  402. bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
  403. /* If the current thread has the trap bit set (i.e. a debugger had
  404. * single stepped the process to the system call), we need to clear
  405. * the trap flag from the new frame. Otherwise, the new thread will
  406. * receive a (likely unexpected) SIGTRAP when it executes the first
  407. * instruction after returning to userland.
  408. */
  409. td->td_frame->tf_eflags &= ~PSL_T;
  410. /*
  411. * Set registers for trampoline to user mode. Leave space for the
  412. * return address on stack. These are the kernel mode register values.
  413. */
  414. pcb2->pcb_edi = 0;
  415. pcb2->pcb_esi = (int)fork_return; /* trampoline arg */
  416. pcb2->pcb_ebp = 0;
  417. pcb2->pcb_esp = (int)td->td_frame - sizeof(void *); /* trampoline arg */
  418. pcb2->pcb_ebx = (int)td; /* trampoline arg */
  419. pcb2->pcb_eip = (int)fork_trampoline + setidt_disp;
  420. pcb2->pcb_gs = rgs();
  421. /*
  422. * If we didn't copy the pcb, we'd need to do the following registers:
  423. * pcb2->pcb_cr3: cloned above.
  424. * pcb2->pcb_dr*: cloned above.
  425. * pcb2->pcb_savefpu: cloned above.
  426. * pcb2->pcb_flags: cloned above.
  427. * pcb2->pcb_onfault: cloned above (always NULL here?).
  428. * pcb2->pcb_gs: cloned above.
  429. * pcb2->pcb_ext: cleared below.
  430. */
  431. pcb2->pcb_ext = NULL;
  432. /* Setup to release spin count in fork_exit(). */
  433. td->td_md.md_spinlock_count = 1;
  434. td->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
  435. }
  436. /*
  437. * Set that machine state for performing an upcall that starts
  438. * the entry function with the given argument.
  439. */
  440. void
  441. cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
  442. stack_t *stack)
  443. {
  444. /*
  445. * Do any extra cleaning that needs to be done.
  446. * The thread may have optional components
  447. * that are not present in a fresh thread.
  448. * This may be a recycled thread so make it look
  449. * as though it's newly allocated.
  450. */
  451. cpu_thread_clean(td);
  452. /*
  453. * Set the trap frame to point at the beginning of the entry
  454. * function.
  455. */
  456. td->td_frame->tf_ebp = 0;
  457. td->td_frame->tf_esp =
  458. (((int)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
  459. td->td_frame->tf_eip = (int)entry;
  460. /* Return address sentinel value to stop stack unwinding. */
  461. suword((void *)td->td_frame->tf_esp, 0);
  462. /* Pass the argument to the entry point. */
  463. suword((void *)(td->td_frame->tf_esp + sizeof(void *)),
  464. (int)arg);
  465. }
  466. int
  467. cpu_set_user_tls(struct thread *td, void *tls_base)
  468. {
  469. struct segment_descriptor sd;
  470. uint32_t base;
  471. /*
  472. * Construct a descriptor and store it in the pcb for
  473. * the next context switch. Also store it in the gdt
  474. * so that the load of tf_fs into %fs will activate it
  475. * at return to userland.
  476. */
  477. base = (uint32_t)tls_base;
  478. sd.sd_lobase = base & 0xffffff;
  479. sd.sd_hibase = (base >> 24) & 0xff;
  480. sd.sd_lolimit = 0xffff; /* 4GB limit, wraps around */
  481. sd.sd_hilimit = 0xf;
  482. sd.sd_type = SDT_MEMRWA;
  483. sd.sd_dpl = SEL_UPL;
  484. sd.sd_p = 1;
  485. sd.sd_xx = 0;
  486. sd.sd_def32 = 1;
  487. sd.sd_gran = 1;
  488. critical_enter();
  489. /* set %gs */
  490. td->td_pcb->pcb_gsd = sd;
  491. if (td == curthread) {
  492. PCPU_GET(fsgs_gdt)[1] = sd;
  493. load_gs(GSEL(GUGS_SEL, SEL_UPL));
  494. }
  495. critical_exit();
  496. return (0);
  497. }
  498. /*
  499. * Convert kernel VA to physical address
  500. */
  501. vm_paddr_t
  502. kvtop(void *addr)
  503. {
  504. vm_paddr_t pa;
  505. pa = pmap_kextract((vm_offset_t)addr);
  506. if (pa == 0)
  507. panic("kvtop: zero page frame");
  508. return (pa);
  509. }
  510. /*
  511. * Get an sf_buf from the freelist. May block if none are available.
  512. */
  513. void
  514. sf_buf_map(struct sf_buf *sf, int flags)
  515. {
  516. pt_entry_t opte, *ptep;
  517. /*
  518. * Update the sf_buf's virtual-to-physical mapping, flushing the
  519. * virtual address from the TLB. Since the reference count for
  520. * the sf_buf's old mapping was zero, that mapping is not
  521. * currently in use. Consequently, there is no need to exchange
  522. * the old and new PTEs atomically, even under PAE.
  523. */
  524. ptep = vtopte(sf->kva);
  525. opte = *ptep;
  526. *ptep = VM_PAGE_TO_PHYS(sf->m) | PG_RW | PG_V |
  527. pmap_cache_bits(kernel_pmap, sf->m->md.pat_mode, 0);
  528. /*
  529. * Avoid unnecessary TLB invalidations: If the sf_buf's old
  530. * virtual-to-physical mapping was not used, then any processor
  531. * that has invalidated the sf_buf's virtual address from its TLB
  532. * since the last used mapping need not invalidate again.
  533. */
  534. #ifdef SMP
  535. if ((opte & (PG_V | PG_A)) == (PG_V | PG_A))
  536. CPU_ZERO(&sf->cpumask);
  537. sf_buf_shootdown(sf, flags);
  538. #else
  539. if ((opte & (PG_V | PG_A)) == (PG_V | PG_A))
  540. pmap_invalidate_page(kernel_pmap, sf->kva);
  541. #endif
  542. }
  543. #ifdef SMP
  544. static void
  545. sf_buf_shootdown_curcpu_cb(pmap_t pmap __unused,
  546. vm_offset_t addr1 __unused, vm_offset_t addr2 __unused)
  547. {
  548. }
  549. void
  550. sf_buf_shootdown(struct sf_buf *sf, int flags)
  551. {
  552. cpuset_t other_cpus;
  553. u_int cpuid;
  554. sched_pin();
  555. cpuid = PCPU_GET(cpuid);
  556. if (!CPU_ISSET(cpuid, &sf->cpumask)) {
  557. CPU_SET(cpuid, &sf->cpumask);
  558. invlpg(sf->kva);
  559. }
  560. if ((flags & SFB_CPUPRIVATE) == 0) {
  561. other_cpus = all_cpus;
  562. CPU_CLR(cpuid, &other_cpus);
  563. CPU_NAND(&other_cpus, &sf->cpumask);
  564. if (!CPU_EMPTY(&other_cpus)) {
  565. CPU_OR(&sf->cpumask, &other_cpus);
  566. smp_masked_invlpg(other_cpus, sf->kva, kernel_pmap,
  567. sf_buf_shootdown_curcpu_cb);
  568. }
  569. }
  570. sched_unpin();
  571. }
  572. #endif
  573. /*
  574. * MD part of sf_buf_free().
  575. */
  576. int
  577. sf_buf_unmap(struct sf_buf *sf)
  578. {
  579. return (0);
  580. }
  581. static void
  582. sf_buf_invalidate(struct sf_buf *sf)
  583. {
  584. vm_page_t m = sf->m;
  585. /*
  586. * Use pmap_qenter to update the pte for
  587. * existing mapping, in particular, the PAT
  588. * settings are recalculated.
  589. */
  590. pmap_qenter(sf->kva, &m, 1);
  591. pmap_invalidate_cache_range(sf->kva, sf->kva + PAGE_SIZE);
  592. }
  593. /*
  594. * Invalidate the cache lines that may belong to the page, if
  595. * (possibly old) mapping of the page by sf buffer exists. Returns
  596. * TRUE when mapping was found and cache invalidated.
  597. */
  598. boolean_t
  599. sf_buf_invalidate_cache(vm_page_t m)
  600. {
  601. return (sf_buf_process_page(m, sf_buf_invalidate));
  602. }
  603. /*
  604. * Software interrupt handler for queued VM system processing.
  605. */
  606. void
  607. swi_vm(void *dummy)
  608. {
  609. if (busdma_swi_pending != 0)
  610. busdma_swi();
  611. }
  612. /*
  613. * Tell whether this address is in some physical memory region.
  614. * Currently used by the kernel coredump code in order to avoid
  615. * dumping the ``ISA memory hole'' which could cause indefinite hangs,
  616. * or other unpredictable behaviour.
  617. */
  618. int
  619. is_physical_memory(vm_paddr_t addr)
  620. {
  621. #ifdef DEV_ISA
  622. /* The ISA ``memory hole''. */
  623. if (addr >= 0xa0000 && addr < 0x100000)
  624. return 0;
  625. #endif
  626. /*
  627. * stuff other tests for known memory-mapped devices (PCI?)
  628. * here
  629. */
  630. return 1;
  631. }