aboutsummaryrefslogtreecommitdiff
path: root/sys/i386/i386/sys_machdep.c
Commit message (Collapse)AuthorAgeFilesLines
* x86: Always use clean FPU and segment base state for new kthreads.John Baldwin2021-03-121-2/+4
| | | | | | | Reviewed by: kib MFC after: 1 week Sponsored by: Netflix Differential Revision: https://reviews.freebsd.org/D29208
* i386: clean up empty lines in .c and .h filesMateusz Guzik2020-09-011-1/+1
| | | | Notes: svn path=/head/; revision=365072
* Use NLDT to get number of LDTs on i386Dimitry Andric2019-02-041-1/+1
| | | | | | | | | | | | | | | | | | | | | | | Compiling a GENERIC kernel for i386 with clang 8.0 results in the following warning: /usr/src/sys/i386/i386/sys_machdep.c:542:40: error: 'sizeof ((ldt))' will return the size of the pointer, not the array itself [-Werror,-Wsizeof-pointer-div] nldt = pldt != NULL ? pldt->ldt_len : nitems(ldt); ^~~~~~~~~~~ /usr/src/sys/sys/param.h:299:32: note: expanded from macro 'nitems' #define nitems(x) (sizeof((x)) / sizeof((x)[0])) ~~~~~~~~~~~ ^ Indeed, 'ldt' is declared as 'union descriptor *', so nitems() is not the right way to determine the number of LDTs. Instead, the NLDT define from sys/x86/include/segments.h should be used. Reviewed by: kib MFC after: 3 days Differential Revision: https://reviews.freebsd.org/D19074 Notes: svn path=/head/; revision=343748
* i386 4/4G split.Konstantin Belousov2018-04-131-12/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The change makes the user and kernel address spaces on i386 independent, giving each almost the full 4G of usable virtual addresses except for one PDE at top used for trampoline and per-CPU trampoline stacks, and system structures that must be always mapped, namely IDT, GDT, common TSS and LDT, and process-private TSS and LDT if allocated. By using 1:1 mapping for the kernel text and data, it appeared possible to eliminate assembler part of the locore.S which bootstraps initial page table and KPTmap. The code is rewritten in C and moved into the pmap_cold(). The comment in vmparam.h explains the KVA layout. There is no PCID mechanism available in protected mode, so each kernel/user switch forth and back completely flushes the TLB, except for the trampoline PTD region. The TLB invalidations for userspace becomes trivial, because IPI handlers switch page tables. On the other hand, context switches no longer need to reload %cr3. copyout(9) was rewritten to use vm_fault_quick_hold(). An issue for new copyout(9) is compatibility with wiring user buffers around sysctl handlers. This explains two kind of locks for copyout ptes and accounting of the vslock() calls. The vm_fault_quick_hold() AKA slow path, is only tried after the 'fast path' failed, which temporary changes mapping to the userspace and copies the data to/from small per-cpu buffer in the trampoline. If a page fault occurs during the copy, it is short-circuit by exception.s to not even reach C code. The change was motivated by the need to implement the Meltdown mitigation, but instead of KPTI the full split is done. The i386 architecture already shows the sizing problems, in particular, it is impossible to link clang and lld with debugging. I expect that the issues due to the virtual address space limits would only exaggerate and the split gives more liveness to the platform. Tested by: pho Discussed with: bde Sponsored by: The FreeBSD Foundation MFC after: 1 month Differential revision: https://reviews.freebsd.org/D14633 Notes: svn path=/head/; revision=332489
* sys: further adoption of SPDX licensing ID tags.Pedro F. Giffuni2017-11-201-0/+2
| | | | | | | | | | | | | | | | | Mainly focus on files that use BSD 3-Clause license. The Software Package Data Exchange (SPDX) group provides a specification to make it easier for automated tools to detect and summarize well known opensource licenses. We are gradually adopting the specification, noting that the tags are considered only advisory and do not, in any way, superceed or replace the license texts. Special thanks to Wind River for providing access to "The Duke of Highlander" tool: an older (2014) run over FreeBSD tree was useful as a starting point. Notes: svn path=/head/; revision=326023
* Change i386_get_ldt() to return 'EOF' when the requested range ofKonstantin Belousov2017-10-091-12/+9
| | | | | | | | | | | | | | | | | descriptors does not fit into currently allocated LDT, or trim the return if the range fits partially. Before, the function returned EINVAL. Fix two bugs in r324366: use capped num counter for malloc size, and do not leak allocated buffer on EINVAL (by handling EINVAL case as normal, see above). Reviewed by: bde Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=324438
* Improvements to set_user_ldt().Konstantin Belousov2017-10-091-27/+26
| | | | | | | | | | | | | | | | | | | Remove mtx_owned() checks from set_user_ldt(). Split the function into _locked() version which requires the dt_lock spinlock owned, and make set_user_ldt() a wrapper. Add a comment in swtch.s noting that the call to the new set_user_ldt() cannot recurse on dt_lock. Remove #ifdef SMP block, the addend is always zero on UP. Fix type of set_user_ldt_rv(), making it match the type used for smb_rendezvous() callback, and remove the cast. Use curproc. Reviewed by: bde Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=324437
* Reset the fs and gs bases on exec(2).Konstantin Belousov2017-10-091-18/+40
| | | | | | | | | | | | | | | The values from the old address space do not make sense for the new program. In particular, gsbase might be the TLS base for the old program but the new program has no TLS now. amd64 already handles this correctly. Reported and reviewed by: bde Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=324433
* More style.Konstantin Belousov2017-10-091-3/+7
| | | | | | | | | Reviewed by: bde Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=324432
* Improve i386_get_ldt().Konstantin Belousov2017-10-061-25/+22
| | | | | | | | | | | | | | | | | | | | | | | | Provide consistent snapshot of the requested descriptors by preventing other threads from modifying LDT while we fetch the data, lock dt_lock around the read. Copy the data into intermediate buffer, which is copied out after the lock is dropped. Comparing with the amd64 version, the read is done byte by byte, since there is no atomic 64bit read (cmpxchg8b method is too heavy comparing with the avoided issues). Improve overflow checking for the descriptors range calculations and remove unneeded casts. Use unsigned types for sizes. Allow zero num argument to i386_get_ldt() and i386_set_ldt(). This case is handled naturally by the code flow. Reviewed by: bde Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=324366
* Remove unneeded cast.Konstantin Belousov2017-10-061-2/+1
| | | | | | | | | Reviewed by: bde Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=324355
* Style.Konstantin Belousov2017-10-061-3/+4
| | | | | | | | | Reviewed by: bde Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=324354
* Use ANSI C declarations.Konstantin Belousov2017-10-051-7/+3
| | | | | | | | | Reviewed by: bde Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=324334
* Correct format specifiers in the debug code. Style.Konstantin Belousov2017-10-051-4/+4
| | | | | | | | | Requested by: bde Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=324330
* Style.Konstantin Belousov2017-10-051-15/+8
| | | | | | | | | Reviewed by: bde Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=324326
* Do not do torn writes to active LDTs.Konstantin Belousov2017-09-191-11/+23
| | | | | | | | | | | | | | | Care must be taken when updating the active LDT, since parallel threads might try to load a segment descriptor which is currently updated. Since the results are undefined, this cannot be ignored by claiming to be an application race. Reviewed by: jhb Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D12413 Notes: svn path=/head/; revision=323772
* Remove register keyword from sys/ and ANSIfy prototypesEd Maste2017-05-171-3/+1
| | | | | | | | | | | | | | | A long long time ago the register keyword told the compiler to store the corresponding variable in a CPU register, but it is not relevant for any compiler used in the FreeBSD world today. ANSIfy related prototypes while here. Reviewed by: cem, jhb Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D10193 Notes: svn path=/head/; revision=318389
* MFamd64: r266901Tijl Coosemans2016-09-251-1/+1
| | | | | | | | | | | | | | | | Allocate a zeroed LDT. Failing to do this might result in the LDT appearing to run out of free descriptors because of random junk in the descriptor's 'sd_type' field. http://lists.freebsd.org/pipermail/freebsd-amd64/2014-May/016088.html PR: 212639 Submitted by: wheelcomplex@gmail.com MFC after: 2 weeks Notes: svn path=/head/; revision=306318
* Check for overflow and return EINVAL if detected. Backport this andKonstantin Belousov2016-05-201-2/+4
| | | | | | | | | | | | r300305 to i386. PR: 209661 Reported and reviewed by: cturt Sponsored by: The FreeBSD Foundation MFC after: 3 days Notes: svn path=/head/; revision=300332
* sys: use our roundup2/rounddown2() macros when param.h is available.Pedro F. Giffuni2016-04-211-1/+1
| | | | | | | | | | | | | rounddown2 tends to produce longer lines than the original code and when the code has a high indentation level it was not really advantageous to do the replacement. This tries to strike a balance between readability using the macros and flexibility of having the expressions, so not everything is converted. Notes: svn path=/head/; revision=298433
* Set %esp correctly in the extended TSS.John Baldwin2015-12-071-2/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The pcb is saved at the top of the kernel stack on x86 platforms. The initial kenrel stack pointer is set in the TSS so that the trapframe from user -> kernel transitions begins directly below the pcb and grows down. The XSAVE changes moved the FPU save area out of the pcb and into a variable-sized area after the pcb. This required updating the expressions to calculate the initial stack pointer from 'stacktop - sizeof(pcb)' to 'stacktop - sizeof(pcb) + FPU save area size'. The i386_set_ioperm() system call allows user applications to access individual I/O ports via the I/O port permission bitmap in the TSS. On FreeBSD this requires allocating a custom per-process TSS instead of using the shared per-CPU TSS. The expression to initialize the initial kernel stack pointer in the per-process TSS created for i386_set_ioperm() was not properly updated after the XSAVE changes. Processes that used i386_set_ioperm() would trash the trapframe during subsequent context switches resulting in panics from memory corruption. This changes fixes the kernel stack pointer calculation for the per-process TSS. Reviewed by: kib, n_hibma Reported by: n_hibma MFC after: 1 week Notes: svn path=/head/; revision=291947
* Make kstack_pages a tunable on arm, x86, and powepc. On i386, theKonstantin Belousov2015-08-101-1/+1
| | | | | | | | | | | | | | | | | | | | | | initial thread stack is not adjusted by the tunable, the stack is allocated too early to get access to the kernel environment. See TD0_KSTACK_PAGES for the thread0 stack sizing on i386. The tunable was tested on x86 only. From the visual inspection, it seems that it might work on arm and powerpc. The arm USPACE_SVC_STACK_TOP and powerpc USPACE macros seems to be already incorrect for the threads with non-default kstack size. I only changed the macros to use variable instead of constant, since I cannot test. On arm64, mips and sparc64, some static data structures are sized by KSTACK_PAGES, so the tunable is disabled. Sponsored by: The FreeBSD Foundation MFC after: 2 week Notes: svn path=/head/; revision=286584
* Reduce code duplication. Add helper fill_based_sd(9) which creates aKonstantin Belousov2015-06-291-23/+20
| | | | | | | | | | based user data descriptor covering whole VA. Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Notes: svn path=/head/; revision=284918
* Remove support for Xen PV domU kernels. Support for HVM domU kernelsJohn Baldwin2015-04-301-103/+0
| | | | | | | | | | | | | | | | | | | | | | | | remains. Xen is planning to phase out support for PV upstream since it is harder to maintain and has more overhead. Modern x86 CPUs include virtualization extensions that support HVM guests instead of PV guests. In addition, the PV code was i386 only and not as well maintained recently as the HVM code. - Remove the i386-only NATIVE option that was used to disable certain components for PV kernels. These components are now standard as they are on amd64. - Remove !XENHVM bits from PV drivers. - Remove various shims required for XEN (e.g. PT_UPDATES_FLUSH, LOAD_CR3, etc.) - Remove duplicate copy of <xen/features.h>. - Remove unused, i386-only xenstored.h. Differential Revision: https://reviews.freebsd.org/D2362 Reviewed by: royger Tested by: royger (i386/amd64 HVM domU and amd64 PVH dom0) Relnotes: yes Notes: svn path=/head/; revision=282274
* MFamd64: Add support for extended FPU states on i386. This includesJohn Baldwin2014-11-021-0/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | support for AVX on i386. - Similar to amd64, move the FPU save area out of the PCB and instead store saved FPU state in a variable-sized buffer after the PCB on the stack. - To support the variable PCB location, alter the locore code to only use the bottom-most page of proc0stack for init386(). init386() returns the correct stack pointer to locore which adjusts the stack for thread0 before calling mi_startup(). - Don't bother setting cr3 in thread0's pcb in locore before calling init386(). It wasn't used (init386() overwrote it at the end) and it doesn't work with the variable-sized FPU save area. - Remove the new-bus attachment from npx. This was only ever useful for external co-processors using IRQ13, but those have not been supported for several years. npxinit() is now called much earlier during boot (init386()) similar to amd64. - Implement PT_{GET,SET}XSTATE and I386_GET_XFPUSTATE. - npxsave() is now only called from context switch contexts so it can use XSAVEOPT. Differential Revision: https://reviews.freebsd.org/D1058 Reviewed by: kib Tested on: FreeBSD/i386 VM under bhyve on Intel i5-2520 Notes: svn path=/head/; revision=273995
* Fix some cosmetic issues with the use of kmem_malloc() in the i386 LDTKonstantin Belousov2014-06-151-24/+6
| | | | | | | | | | | | | | | sysarch(2) code. Use M_ZERO instead of explicit bzero(9). Do not check for failed allocation when M_WAITOK is specified (which is specified always). Use malloc(9) when allocating memory for the intermediate copy of the user-supplied buffer. Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=267492
* Update kernel inclusions of capability.h to use capsicum.h instead; someRobert Watson2014-03-161-1/+1
| | | | | | | | | | | further refinement is required as some device drivers intended to be portable over FreeBSD versions rely on __FreeBSD_version to decide whether to include capability.h. MFC after: 3 weeks Notes: svn path=/head/; revision=263233
* Remove clause 3 (the advertising clause), per the regent's letter.Warner Losh2014-03-111-1/+1
| | | | Notes: svn path=/head/; revision=263037
* Fix panic in ktrcapfail() when no capability rights are passed.Pawel Jakub Dawidek2013-09-181-1/+1
| | | | | | | | | | | | While here, correct all consumers to pass NULL instead of 0 as we pass capability rights as pointers now, not uint64_t. Reported by: Daniel Peyrolon Tested by: Daniel Peyrolon Approved by: re (marius) Notes: svn path=/head/; revision=255677
* Replace kernel virtual address space allocation with vmem. This providesJeff Roberson2013-08-071-8/+8
| | | | | | | | | | | | | | | | transparent layering and better fragmentation. - Normalize functions that allocate memory to use kmem_* - Those that allocate address space are named kva_* - Those that operate on maps are named kmap_* - Implement recursive allocation handling for kmem_arena in vmem. Reviewed by: alc Tested by: pho Sponsored by: EMC / Isilon Storage Division Notes: svn path=/head/; revision=254025
* - Use kmem_malloc rather than kmem_alloc() for GDT/LDT/tss allocations etc.Jeff Roberson2013-07-261-7/+9
| | | | | | | | | | | | This eliminates some unusual uses of that API in favor of more typical uses of kmem_malloc(). Discussed with: kib/alc Tested by: pho Sponsored by: EMC / Isilon Storage Division Notes: svn path=/head/; revision=253685
* Use curthread rather than PCPU_GET(curthread). 'curthread' usesJohn Baldwin2011-12-291-1/+1
| | | | | | | | | | special-case optimizations on several platforms and is preferred. Reported by: dim (indirectly) MFC after: 2 weeks Notes: svn path=/head/; revision=228962
* Trace attempts to call restricted MD syscalls.Dag-Erling Smørgrav2011-10-181-0/+4
| | | | Notes: svn path=/head/; revision=226498
* Add some checks to ensure that Capsicum is behaving correctly, and add someJonathan Anderson2011-06-301-12/+14
| | | | | | | | | | more explicit comments about what's going on and what future maintainers need to do when e.g. adding a new operation to a sys_machdep.c. Approved by: mentor(rwatson), re(bz) Notes: svn path=/head/; revision=223692
* We may split today's CAPABILITIES into CAPABILITY_MODE (which hasJonathan Anderson2011-06-291-2/+2
| | | | | | | | | | | | | | | | to do with global namespaces) and CAPABILITIES (which has to do with constraining file descriptors). Just in case, and because it's a better name anyway, let's move CAPABILITIES out of the way. Also, change opt_capabilities.h to opt_capsicum.h; for now, this will only hold CAPABILITY_MODE, but it will probably also hold the new CAPABILITIES (implying constrained file descriptors) in the future. Approved by: rwatson Sponsored by: Google UK Ltd Notes: svn path=/head/; revision=223668
* Continue to introduce Capsicum capability mode:Robert Watson2011-03-011-0/+25
| | | | | | | | | | | | | | | White list sysarch calls allowed in capability mode; arguably, there should be some link between the capability mode model and the privilege model here. Sysarch is a morass similar to ioctl, in many senses. Submitted by: anderson Discussed with: benl, kris, pjd Sponsored by: Google, Inc. Obtained from: Capsicum Project MFC after: 3 months Notes: svn path=/head/; revision=219134
* - Remove some always-true checks (checking for unsigned < 0).John Baldwin2011-01-181-3/+2
| | | | | | | | | | | | | - Only check largs->num against max_ldt_segment on amd64 for I386_SET_LDT when descriptors are provided. Specifically, allow the 'start == 0' and 'num == 0' special case used to free all LDT entries that previously failed with EINVAL. Submitted by: clang via rdivacky (some of 1) Reviewed by: kib Notes: svn path=/head/; revision=217543
* Make i386_set_ldt work on i386/XEN, step 5/5.Colin Percival2010-12-311-0/+5
| | | | | | | | | | | | When cleaning up a thread, reset its LDT to the default LDT. Note: Casting the LDT pointer to an int and storing it in pc_currentldt is wildly bogus, but is harmless since pc_currentldt is a write-only variable. MFC after: 3 days Notes: svn path=/head/; revision=216847
* Make i386_set_ldt work on i386/XEN, step 4/5.Colin Percival2010-12-311-4/+8
| | | | | | | | | | | | | | | | | Use xen_update_descriptor to update the LDT rather than bcopy. Under Xen, pages used for holding LDTs must be read-only, so we can't make the change ourselves. Ths obvious alternative of "remap the page read-write, make the change, then map it read-only again" doesn't work since Xen won't allow an LDT page to be remapped as R/W. An arguably better solution is used by NetBSD: They don't modify LDTs in-place at all, but instead copy the entire LDT, modify the new version, then atomically swap. MFC after: 3 days Notes: svn path=/head/; revision=216846
* Make i386_set_ldt work on i386/XEN, step 3/5.Colin Percival2010-12-311-0/+3
| | | | | | | | | | | | Synchronize reality with comment: The user_ldt_alloc function is supposed to return with dt_lock held. Due to broken locking in i386/xen/pmap.c, we drop dt_lock during the call to pmap_map_readonly and then pick it up again; this can be removed once the Xen pmap locking is fixed. MFC after: 3 days Notes: svn path=/head/; revision=216845
* Replace AUDIT_ARG() with variable argument macros with a set more moreRobert Watson2009-06-271-1/+1
| | | | | | | | | | | | | | | | | specific macros for each audit argument type. This makes it easier to follow call-graphs, especially for automated analysis tools (such as fxr). In MFC, we should leave the existing AUDIT_ARG() macros as they may be used by third-party kernel modules. Suggested by: brooks Approved by: re (kib) Obtained from: TrustedBSD Project MFC after: 1 week Notes: svn path=/head/; revision=195104
* Move "options MAC" from opt_mac.h to opt_global.h, as it's now in GENERICRobert Watson2009-06-051-1/+0
| | | | | | | | | | | and used in a large number of files, but also because an increasing number of incorrect uses of MAC calls were sneaking in due to copy-and-paste of MAC-aware code without the associated opt_mac.h include. Discussed with: pjd Notes: svn path=/head/; revision=193511
* Retire the MALLOC and FREE macros. They are an abomination unto style(9).Dag-Erling Smørgrav2008-10-231-7/+7
| | | | | | | MFC after: 3 months Notes: svn path=/head/; revision=184205
* When doing rfork(0), i.e. separating curproc VM from any other user ofKonstantin Belousov2008-09-121-0/+8
| | | | | | | | | | | | | | | the same vmspace, decrement the reference count of the shared LDT instead of a newly-made copy. Code factually removed LDT from the process that did rfork(0). Introduce user_ldt_deref() function that does decrement of refcount for the struct proc_ldt, and call it in the rfork(0) case on the shared LDT. Reviewed by: jhb MFC after: 1 week Notes: svn path=/head/; revision=182961
* The user_ldt_alloc() function shall return with dt_lock locked.Konstantin Belousov2008-09-121-5/+9
| | | | | | | | | | | | | The user_ldt_free() function shall return with dt_lock unlocked. Error handling code in both functions do not handle this, fix it by doing necessary lock/unlock. While there, fix minor style nits. MFC after: 1 week Notes: svn path=/head/; revision=182960
* Remove warning about static LDT segment allocation. ApplicationsKonstantin Belousov2008-09-121-9/+0
| | | | | | | | | | continue using it after ~7 years since warning was introduced, and there is no reason to discourage them. MFC after: 1 week Notes: svn path=/head/; revision=182959
* Integrate support for xen in to i386 common code.Kip Macy2008-08-151-0/+96
| | | | | | | MFC after: 1 month Notes: svn path=/head/; revision=181775
* Remove kernel support for M:N threading.Jeff Roberson2008-03-121-3/+0
| | | | | | | | | | | While the KSE project was quite successful in bringing threading to FreeBSD, the M:N approach taken by the kse library was never developed to its full potential. Backwards compatibility will be provided via libmap.conf for dynamically linked binaries and static binaries will be broken. Notes: svn path=/head/; revision=177091
* NULL_LDT_BASE is used in !SMP kernels too and set_user_ldt() is notAttilio Rao2007-07-081-4/+2
| | | | | | | | | | | | properly called. Address these two issues. Reported by: Tinderbox Tested by: le Approved by: jeff (mentor) Approved by: re Notes: svn path=/head/; revision=171309
* Actual code shows several problems in ia32 LDT handling:Attilio Rao2007-07-071-32/+38
| | | | | | | | | | | | | | | | | | | | | | | - When a LDT entry changes, the old one is freed while it is still referenced by gdt and ldtr. This can lead to disruptive behaviours in particular on SMP machines. - When a LDT entry changes, it is assumed that the only one entity sharing the same LDT are threads in the same proc. It doesn't take in account edge cases where two processes share the same VM (rfork'ed ones, for example). This patch addresses these two problems and addictionally it fixes the usage of refcount switching back it to the old manually-grown refcount (since in this case would be faster). Diagnosed by: tegge Tested by: pho (a former version) Reviewed by: kib Approved by: jeff (mentor) Approved by: re Notes: svn path=/head/; revision=171295