Skip to content

Commit

Permalink
xnu-7195.121.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Darwin authored and das committed Jul 6, 2021
1 parent a1babec commit 2ff845c
Show file tree
Hide file tree
Showing 132 changed files with 2,622 additions and 1,385 deletions.
24 changes: 24 additions & 0 deletions bsd/dev/i386/sysctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,30 @@ extern uint64_t x86_isr_fp_simd_use;
SYSCTL_QUAD(_machdep, OID_AUTO, x86_fp_simd_isr_uses,
CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
&x86_isr_fp_simd_use, "");

static int
sysctl_kern_insn_copy_optout_task SYSCTL_HANDLER_ARGS
{
#pragma unused(oidp, arg1, arg2)
uint32_t soflags = 0;
uint32_t old_value = curtask_get_insn_copy_optout() ? 1 : 0;

int error = SYSCTL_IN(req, &soflags, sizeof(soflags));
if (error) {
return error;
}

if (soflags) {
curtask_set_insn_copy_optout();
}

return SYSCTL_OUT(req, &old_value, sizeof(old_value));
}
SYSCTL_PROC(_machdep, OID_AUTO, insn_copy_optout_task,
CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_MASKED | CTLFLAG_ANYBODY,
0, 0, sysctl_kern_insn_copy_optout_task, "I", "");


#if DEVELOPMENT || DEBUG

extern int plctrace_enabled;
Expand Down
1 change: 1 addition & 0 deletions bsd/kern/kern_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ cs_allow_invalid(struct proc *p)
task_set_memory_ownership_transfer(p->task, TRUE);

vm_map_switch_protect(get_task_map(p->task), FALSE);
vm_map_cs_debugged_set(get_task_map(p->task), TRUE);
#endif
return (p->p_csflags & (CS_KILL | CS_HARD)) == 0;
}
Expand Down
31 changes: 22 additions & 9 deletions bsd/kern/kern_descrip.c
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ sys_fcntl_nocancel(proc_t p, struct fcntl_nocancel_args *uap, int32_t *retval)
}

#if CONFIG_MACF
error = mac_file_check_fcntl(proc_ucred(p), fp->fp_glob, uap->cmd,
error = mac_file_check_fcntl(kauth_cred_get(), fp->fp_glob, uap->cmd,
uap->arg);
if (error) {
goto out;
Expand Down Expand Up @@ -1258,7 +1258,7 @@ sys_fcntl_nocancel(proc_t p, struct fcntl_nocancel_args *uap, int32_t *retval)
}

#if CONFIG_MACF
error = mac_file_check_lock(proc_ucred(p), fp->fp_glob,
error = mac_file_check_lock(kauth_cred_get(), fp->fp_glob,
F_SETLK, &fl);
if (error) {
(void)vnode_put(vp);
Expand Down Expand Up @@ -1404,7 +1404,7 @@ sys_fcntl_nocancel(proc_t p, struct fcntl_nocancel_args *uap, int32_t *retval)
}

#if CONFIG_MACF
error = mac_file_check_lock(proc_ucred(p), fp->fp_glob,
error = mac_file_check_lock(kauth_cred_get(), fp->fp_glob,
uap->cmd, &fl);
if (error == 0)
#endif
Expand Down Expand Up @@ -2701,7 +2701,7 @@ sys_fcntl_nocancel(proc_t p, struct fcntl_nocancel_args *uap, int32_t *retval)

#if CONFIG_MACF
/* Re-do MAC checks against the new FD, pass in a fake argument */
error = mac_file_check_fcntl(proc_ucred(p), fp2->fp_glob, uap->cmd, 0);
error = mac_file_check_fcntl(kauth_cred_get(), fp2->fp_glob, uap->cmd, 0);
if (error) {
fp_drop(p, fd2, fp2, 1);
goto out;
Expand Down Expand Up @@ -3212,6 +3212,7 @@ finishdup(proc_t p,
struct fileproc *ofp;
#if CONFIG_MACF
int error;
kauth_cred_t cred;
#endif

#if DIAGNOSTIC
Expand All @@ -3224,7 +3225,9 @@ finishdup(proc_t p,
}

#if CONFIG_MACF
error = mac_file_check_dup(proc_ucred(p), ofp->fp_glob, new);
cred = kauth_cred_proc_ref(p);
error = mac_file_check_dup(cred, ofp->fp_glob, new);
kauth_cred_unref(&cred);
if (error) {
fdrelse(p, new);
return error;
Expand Down Expand Up @@ -3322,6 +3325,9 @@ fp_close_and_unlock(proc_t p, int fd, struct fileproc *fp, int flags)
{
struct filedesc *fdp = p->p_fd;
struct fileglob *fg = fp->fp_glob;
#if CONFIG_MACF
kauth_cred_t cred;
#endif

#if DIAGNOSTIC
proc_fdlock_assert(p, LCK_MTX_ASSERT_OWNED);
Expand Down Expand Up @@ -3370,7 +3376,9 @@ fp_close_and_unlock(proc_t p, int fd, struct fileproc *fp, int flags)
kauth_authorize_fileop(fg->fg_cred, KAUTH_FILEOP_CLOSE,
(uintptr_t)fg->fg_data, (uintptr_t)fileop_flags);
#if CONFIG_MACF
mac_file_notify_close(proc_ucred(p), fp->fp_glob);
cred = kauth_cred_proc_ref(p);
mac_file_notify_close(cred, fp->fp_glob);
kauth_cred_unref(&cred);
#endif
vnode_put((vnode_t)fg->fg_data);
}
Expand Down Expand Up @@ -4541,6 +4549,9 @@ falloc_withalloc(proc_t p, struct fileproc **resultfp, int *resultfd,
struct fileproc *fp;
struct fileglob *fg;
int error, nfd;
#if CONFIG_MACF
kauth_cred_t cred;
#endif

/* Make sure we don't go beyond the system-wide limit */
if (nfiles >= maxfiles) {
Expand All @@ -4557,7 +4568,9 @@ falloc_withalloc(proc_t p, struct fileproc **resultfp, int *resultfd,
}

#if CONFIG_MACF
error = mac_file_check_create(proc_ucred(p));
cred = kauth_cred_proc_ref(p);
error = mac_file_check_create(cred);
kauth_cred_unref(&cred);
if (error) {
proc_fdunlock(p);
return error;
Expand Down Expand Up @@ -5204,7 +5217,7 @@ sys_flock(proc_t p, struct flock_args *uap, __unused int32_t *retval)
goto out;
}
#if CONFIG_MACF
error = mac_file_check_lock(proc_ucred(p), fp->fp_glob, F_SETLK, &lf);
error = mac_file_check_lock(kauth_cred_get(), fp->fp_glob, F_SETLK, &lf);
if (error) {
goto out;
}
Expand Down Expand Up @@ -5471,7 +5484,7 @@ dupfdopen(struct filedesc *fdp, int indx, int dfd, int flags, int error)
return EBADF;
}
#if CONFIG_MACF
myerror = mac_file_check_dup(proc_ucred(p), wfp->fp_glob, dfd);
myerror = mac_file_check_dup(kauth_cred_get(), wfp->fp_glob, dfd);
if (myerror) {
proc_fdunlock(p);
return myerror;
Expand Down
75 changes: 75 additions & 0 deletions bsd/kern/kern_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
#include <machine/reg.h>
#include <machine/cpu_capabilities.h>

#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/filedesc.h>
Expand All @@ -102,6 +103,7 @@
#include <sys/signal.h>
#include <sys/aio_kern.h>
#include <sys/sysproto.h>
#include <sys/sysctl.h>
#include <sys/persona.h>
#include <sys/reason.h>
#if SYSV_SHM
Expand Down Expand Up @@ -160,6 +162,7 @@
#include <vm/vm_kern.h>
#include <vm/vm_fault.h>
#include <vm/vm_pageout.h>
#include <vm/pmap.h>

#include <kdp/kdp_dyld.h>

Expand All @@ -173,6 +176,7 @@
#endif

#include <IOKit/IOBSD.h>
#include <IOKit/IOPlatformExpert.h>

extern boolean_t vm_darkwake_mode;

Expand Down Expand Up @@ -2928,6 +2932,7 @@ proc_apply_jit_and_jumbo_va_policies(proc_t p, task_t task)
vm_map_set_jumbo(get_task_map(task));
if (jit_entitled) {
vm_map_set_jit_entitled(get_task_map(task));

}
}
}
Expand Down Expand Up @@ -5393,6 +5398,10 @@ exec_extract_strings(struct image_params *imgp)
* System malloc engages nanozone for UIAPP.
*/
#define NANO_ENGAGE_KEY "MallocNanoZone=1"
/*
* Used to pass experiment flags up to libmalloc.
*/
#define LIBMALLOC_EXPERIMENT_FACTORS_KEY "MallocExperiment="

#define PFZ_KEY "pfz="
extern user32_addr_t commpage_text32_location;
Expand Down Expand Up @@ -5420,6 +5429,10 @@ extern uuid_string_t bootsessionuuid_string;
#define HEX_STR_LEN 18 // 64-bit hex value "0x0123456701234567"
#define HEX_STR_LEN32 10 // 32-bit hex value "0x01234567"

#if XNU_TARGET_OS_OSX && _POSIX_SPAWN_FORCE_4K_PAGES && PMAP_CREATE_FORCE_4K_PAGES
#define VM_FORCE_4K_PAGES_KEY "vm_force_4k_pages=1"
#endif /* XNU_TARGET_OS_OSX && _POSIX_SPAWN_FORCE_4K_PAGES && PMAP_CREATE_FORCE_4K_PAGES */

static int
exec_add_entropy_key(struct image_params *imgp,
const char *key,
Expand Down Expand Up @@ -5466,6 +5479,8 @@ is_arm64e_running_as_arm64(const struct image_params *imgp)
}
#endif /* __has_feature(ptrauth_calls) */

_Atomic uint64_t libmalloc_experiment_factors = 0;

static int
exec_add_apple_strings(struct image_params *imgp,
const load_result_t *load_result)
Expand All @@ -5474,6 +5489,7 @@ exec_add_apple_strings(struct image_params *imgp,
int img_ptr_size = (imgp->ip_flags & IMGPF_IS_64BIT_ADDR) ? 8 : 4;
thread_t new_thread;
ipc_port_t sright;
uint64_t local_experiment_factors = 0;

/* exec_save_path stored the first string */
imgp->ip_applec = 1;
Expand Down Expand Up @@ -5700,6 +5716,42 @@ exec_add_apple_strings(struct image_params *imgp,
imgp->ip_applec++;
}

#if XNU_TARGET_OS_OSX && _POSIX_SPAWN_FORCE_4K_PAGES && PMAP_CREATE_FORCE_4K_PAGES
if (imgp->ip_px_sa != NULL) {
struct _posix_spawnattr* psa = (struct _posix_spawnattr *) imgp->ip_px_sa;
if (psa->psa_flags & _POSIX_SPAWN_FORCE_4K_PAGES) {
const char *vm_force_4k_string = VM_FORCE_4K_PAGES_KEY;
error = exec_add_user_string(imgp, CAST_USER_ADDR_T(vm_force_4k_string), UIO_SYSSPACE, FALSE);
if (error) {
goto bad;
}
imgp->ip_applec++;
}
}
#endif /* XNU_TARGET_OS_OSX && _POSIX_SPAWN_FORCE_4K_PAGES && PMAP_CREATE_FORCE_4K_PAGES */

/* adding the libmalloc experiment string */
local_experiment_factors = os_atomic_load_wide(&libmalloc_experiment_factors, relaxed);
if (__improbable(local_experiment_factors != 0)) {
char libmalloc_experiment_factors_string[strlen(LIBMALLOC_EXPERIMENT_FACTORS_KEY) + HEX_STR_LEN + 1];

snprintf(
libmalloc_experiment_factors_string,
sizeof(libmalloc_experiment_factors_string),
LIBMALLOC_EXPERIMENT_FACTORS_KEY "0x%llx",
local_experiment_factors);
error = exec_add_user_string(
imgp,
CAST_USER_ADDR_T(libmalloc_experiment_factors_string),
UIO_SYSSPACE,
FALSE);
if (error) {
printf("Failed to add the libmalloc experiment factors string with error %d\n", error);
goto bad;
}
imgp->ip_applec++;
}

/* Align the tail of the combined applev area */
while (imgp->ip_strspace % img_ptr_size != 0) {
*imgp->ip_strendp++ = '\0';
Expand Down Expand Up @@ -7214,3 +7266,26 @@ exec_prefault_data(proc_t p __unused, struct image_params *imgp, load_result_t *
}
}
}

static int
sysctl_libmalloc_experiments SYSCTL_HANDLER_ARGS
{
#pragma unused(oidp, arg2, req)
int changed;
errno_t error;
uint64_t value = os_atomic_load_wide(&libmalloc_experiment_factors, relaxed);

error = sysctl_io_number(req, value, sizeof(value), &value, &changed);
if (error) {
return error;
}

if (changed) {
os_atomic_store_wide(&libmalloc_experiment_factors, value, relaxed);
}

return 0;
}

EXPERIMENT_FACTOR_PROC(_kern, libmalloc_experiments, CTLTYPE_QUAD | CTLFLAG_RW, 0, 0, &sysctl_libmalloc_experiments, "A", "");

3 changes: 3 additions & 0 deletions bsd/kern/kern_memorystatus.c
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,8 @@ memorystatus_do_kill(proc_t p, uint32_t cause, os_reason_t jetsam_reason, uint64
case kMemorystatusKilledPerProcessLimit: jetsam_flags |= P_JETSAM_PID; break;
case kMemorystatusKilledIdleExit: jetsam_flags |= P_JETSAM_IDLEEXIT; break;
}
/* jetsam_do_kill drops a reference. */
os_reason_ref(jetsam_reason);
error = jetsam_do_kill(p, jetsam_flags, jetsam_reason);
*footprint_of_killed_proc = ((error == 0) ? footprint : 0);

Expand All @@ -1701,6 +1703,7 @@ memorystatus_do_kill(proc_t p, uint32_t cause, os_reason_t jetsam_reason, uint64
KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_COMPACTOR_RUN)) | DBG_FUNC_END,
victim_pid, cause, vm_page_free_count, 0, 0);

os_reason_free(jetsam_reason);
return error == 0;
}

Expand Down
38 changes: 38 additions & 0 deletions bsd/kern/kern_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,24 @@ proc_uniqueid(proc_t p)
return p->p_uniqueid;
}

uint64_t proc_uniqueid_task(void *p_arg, void *t);
/*
* During exec, two tasks point at the proc. This function is used
* to gives tasks a unique ID; we make the matching task have the
* proc's uniqueid, and any other task gets the high-bit flipped.
* (We need to try to avoid returning UINT64_MAX, which is the
* which is the uniqueid of a task without a proc. (e.g. while exiting))
*
* Only used by get_task_uniqueid(); do not add additional callers.
*/
uint64_t
proc_uniqueid_task(void *p_arg, void *t)
{
proc_t p = p_arg;
uint64_t uniqueid = p->p_uniqueid;
return uniqueid ^ (__probable(t == (void *)p->task) ? 0 : (1ull << 63));
}

uint64_t
proc_puniqueid(proc_t p)
{
Expand Down Expand Up @@ -3521,6 +3539,26 @@ proc_resetregister(proc_t p)
proc_unlock(p);
}

bool
proc_get_pthread_jit_allowlist(proc_t p)
{
bool ret = false;

proc_lock(p);
ret = (p->p_lflag & P_LPTHREADJITALLOWLIST);
proc_unlock(p);

return ret;
}

void
proc_set_pthread_jit_allowlist(proc_t p)
{
proc_lock(p);
p->p_lflag |= P_LPTHREADJITALLOWLIST;
proc_unlock(p);
}

pid_t
proc_pgrpid(proc_t p)
{
Expand Down
11 changes: 11 additions & 0 deletions bsd/kern/kern_sysctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -4071,6 +4071,17 @@ SYSCTL_INT(_vm, OID_AUTO, compressor_sample_max_in_msecs, CTLFLAG_RW | CTLFLAG_L
SYSCTL_INT(_vm, OID_AUTO, compressor_thrashing_threshold_per_10msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &compressor_thrashing_threshold_per_10msecs, 0, "");
SYSCTL_INT(_vm, OID_AUTO, compressor_thrashing_min_per_10msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &compressor_thrashing_min_per_10msecs, 0, "");

SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapouts_under_30s, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.unripe_under_30s, "");
SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapouts_under_60s, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.unripe_under_60s, "");
SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapouts_under_300s, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.unripe_under_300s, "");
SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_reclaim_swapins, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.reclaim_swapins, "");
SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_defrag_swapins, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.defrag_swapins, "");
SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_threshold_exceeded, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.compressor_swap_threshold_exceeded, "");
SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_fileq_throttled, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.external_q_throttled, "");
SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_free_count_low, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.free_count_below_reserve, "");
SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_thrashing_detected, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.thrashing_detected, "");
SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_fragmentation_detected, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.fragmentation_detected, "");

SYSCTL_STRING(_vm, OID_AUTO, swapfileprefix, CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED, swapfilename, sizeof(swapfilename) - SWAPFILENAME_INDEX_LEN, "");

SYSCTL_INT(_vm, OID_AUTO, compressor_timing_enabled, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_compressor_time_thread, 0, "");
Expand Down
Loading

0 comments on commit 2ff845c

Please sign in to comment.