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 /*
  23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  28 /*        All Rights Reserved   */
  29 
  30 #include <sys/types.h>
  31 #include <sys/t_lock.h>
  32 #include <sys/param.h>
  33 #include <sys/cred.h>
  34 #include <sys/debug.h>
  35 #include <sys/inline.h>
  36 #include <sys/kmem.h>
  37 #include <sys/proc.h>
  38 #include <sys/regset.h>
  39 #include <sys/privregs.h>
  40 #include <sys/sysmacros.h>
  41 #include <sys/systm.h>
  42 #include <sys/vfs.h>
  43 #include <sys/vnode.h>
  44 #include <sys/psw.h>
  45 #include <sys/pcb.h>
  46 #include <sys/buf.h>
  47 #include <sys/signal.h>
  48 #include <sys/user.h>
  49 #include <sys/cpuvar.h>
  50 
  51 #include <sys/fault.h>
  52 #include <sys/syscall.h>
  53 #include <sys/procfs.h>
  54 #include <sys/cmn_err.h>
  55 #include <sys/stack.h>
  56 #include <sys/debugreg.h>
  57 #include <sys/copyops.h>
  58 
  59 #include <sys/vmem.h>
  60 #include <sys/mman.h>
  61 #include <sys/vmparam.h>
  62 #include <sys/fp.h>
  63 #include <sys/archsystm.h>
  64 #include <sys/vmsystm.h>
  65 #include <vm/hat.h>
  66 #include <vm/as.h>
  67 #include <vm/seg.h>
  68 #include <vm/seg_kmem.h>
  69 #include <vm/seg_kp.h>
 
 
 219 
 220 /*
 221  * Set floating-point registers.
 222  * (Note: This can be an alias to setfpregs().)
 223  */
 224 void
 225 prsetprfpregs(klwp_t *lwp, prfpregset_t *pfp)
 226 {
 227         setfpregs(lwp, pfp);
 228 }
 229 
 230 #if defined(_SYSCALL32_IMPL)
 231 void
 232 prsetprfpregs32(klwp_t *lwp, prfpregset32_t *pfp)
 233 {
 234         setfpregs32(lwp, pfp);
 235 }
 236 #endif  /* _SYSCALL32_IMPL */
 237 
 238 /*
 239  * Does the system support extra register state?
 240  */
 241 /* ARGSUSED */
 242 int
 243 prhasx(proc_t *p)
 244 {
 245         return (0);
 246 }
 247 
 248 /*
 249  * Get the size of the extra registers.
 250  */
 251 /* ARGSUSED */
 252 int
 253 prgetprxregsize(proc_t *p)
 254 {
 255         return (0);
 256 }
 257 
 258 /*
 259  * Get extra registers.
 260  */
 261 /*ARGSUSED*/
 262 void
 263 prgetprxregs(klwp_t *lwp, caddr_t prx)
 264 {
 265         /* no extra registers */
 266 }
 267 
 268 /*
 269  * Set extra registers.
 270  */
 271 /*ARGSUSED*/
 272 void
 273 prsetprxregs(klwp_t *lwp, caddr_t prx)
 274 {
 275         /* no extra registers */
 276 }
 277 
 278 /*
 279  * Return the base (lower limit) of the process stack.
 280  */
 281 caddr_t
 282 prgetstackbase(proc_t *p)
 283 {
 284         return (p->p_usrstack - p->p_stksize);
 285 }
 286 
 287 /*
 288  * Return the "addr" field for pr_addr in prpsinfo_t.
 289  * This is a vestige of the past, so whatever we return is OK.
 290  */
 291 caddr_t
 292 prgetpsaddr(proc_t *p)
 293 {
 294         return ((caddr_t)p);
 295 }
 
 | 
 
 
  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 /*
  23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  28 /*        All Rights Reserved   */
  29 
  30 /*
  31  * Copyright 2023 Oxide Computer Company
  32  */
  33 
  34 #include <sys/types.h>
  35 #include <sys/t_lock.h>
  36 #include <sys/param.h>
  37 #include <sys/cred.h>
  38 #include <sys/debug.h>
  39 #include <sys/inline.h>
  40 #include <sys/kmem.h>
  41 #include <sys/proc.h>
  42 #include <sys/regset.h>
  43 #include <sys/privregs.h>
  44 #include <sys/sysmacros.h>
  45 #include <sys/systm.h>
  46 #include <sys/vfs.h>
  47 #include <sys/vnode.h>
  48 #include <sys/psw.h>
  49 #include <sys/pcb.h>
  50 #include <sys/buf.h>
  51 #include <sys/signal.h>
  52 #include <sys/user.h>
  53 #include <sys/cpuvar.h>
  54 #include <sys/stdalign.h>
  55 
  56 #include <sys/fault.h>
  57 #include <sys/syscall.h>
  58 #include <sys/procfs.h>
  59 #include <sys/cmn_err.h>
  60 #include <sys/stack.h>
  61 #include <sys/debugreg.h>
  62 #include <sys/copyops.h>
  63 
  64 #include <sys/vmem.h>
  65 #include <sys/mman.h>
  66 #include <sys/vmparam.h>
  67 #include <sys/fp.h>
  68 #include <sys/archsystm.h>
  69 #include <sys/vmsystm.h>
  70 #include <vm/hat.h>
  71 #include <vm/as.h>
  72 #include <vm/seg.h>
  73 #include <vm/seg_kmem.h>
  74 #include <vm/seg_kp.h>
 
 
 224 
 225 /*
 226  * Set floating-point registers.
 227  * (Note: This can be an alias to setfpregs().)
 228  */
 229 void
 230 prsetprfpregs(klwp_t *lwp, prfpregset_t *pfp)
 231 {
 232         setfpregs(lwp, pfp);
 233 }
 234 
 235 #if defined(_SYSCALL32_IMPL)
 236 void
 237 prsetprfpregs32(klwp_t *lwp, prfpregset32_t *pfp)
 238 {
 239         setfpregs32(lwp, pfp);
 240 }
 241 #endif  /* _SYSCALL32_IMPL */
 242 
 243 /*
 244  * This is a general function that the main part of /proc and the rest of the
 245  * system uses to ask does a given process actually have extended state. Right
 246  * now, this question is not process-specific, but rather CPU specific. We look
 247  * at whether xsave has been enabled to determine that. While strictly speaking
 248  * one could make the argument that all amd64 CPUs support fxsave and we could
 249  * emulate something that only supports that, we don't think that makes sense.
 250  */
 251 int
 252 prhasx(proc_t *p)
 253 {
 254         return (fpu_xsave_enabled());
 255 }
 256 
 257 /*
 258  * Return the minimum size that we need to determine the full size of a
 259  * prxregset_t.
 260  */
 261 boolean_t
 262 prwriteminxreg(size_t *sizep)
 263 {
 264         *sizep = sizeof (prxregset_hdr_t);
 265         return (B_TRUE);
 266 }
 267 
 268 /*
 269  * This routine services both ILP32 and LP64 callers. We cannot assume anything
 270  * about the alignment of argp and must bcopy things to known structures that we
 271  * care about. We are guaranteed we have prxregset_hdr_t bytes because we asked
 272  * for them above.
 273  */
 274 boolean_t
 275 prwritesizexreg(const void *argp, size_t *sizep)
 276 {
 277         prxregset_hdr_t hdr;
 278 
 279         /*
 280          * While it's tempting to validate everything here, the only thing we
 281          * care about is that we understand the type and the size meets our
 282          * constraints:
 283          *
 284          *  o We actually have an item of type PR_TYPE_XSAVE, otherwise we
 285          *    don't know what this is.
 286          *  o The indicated size actually contains at least the
 287          *    prxregset_hdr_t.
 288          *  o The indicated size isn't larger than what the FPU tells us is
 289          *    allowed.
 290          *
 291          * We do not check if the reset of the structure makes semantic sense at
 292          * this point. We save all other validation for the normal set function
 293          * as that's when we'll have the rest of our data.
 294          */
 295         bcopy(argp, &hdr, sizeof (hdr));
 296         if (hdr.pr_type != PR_TYPE_XSAVE ||
 297             hdr.pr_size > fpu_proc_xregs_max_size() ||
 298             hdr.pr_size < sizeof (prxregset_hdr_t)) {
 299                 return (B_FALSE);
 300         }
 301 
 302         *sizep = hdr.pr_size - sizeof (prxregset_hdr_t);
 303         return (B_TRUE);
 304 }
 305 
 306 /*
 307  * Get the size of the extra registers. The ultimate size here depends on a
 308  * combination of a few different things. Right now the xregs always have our
 309  * header, the illumos-specific XCR information, the xsave information, and then
 310  * otherwise this varies based on the items that the CPU supports.
 311  *
 312  * The ultimate size here is going to be:
 313  *
 314  *  o 1x prxregset_hdr_t
 315  *  o n  prxregset_info_t structures
 316  *  o The individual data for each one
 317  */
 318 size_t
 319 prgetprxregsize(proc_t *p)
 320 {
 321         uint32_t size;
 322 
 323         fpu_proc_xregs_info(p, NULL, &size, NULL);
 324         return (size);
 325 }
 326 
 327 /*
 328  * Get extra registers.
 329  */
 330 void
 331 prgetprxregs(klwp_t *lwp, prxregset_t *prx)
 332 {
 333         fpu_proc_xregs_get(lwp, prx);
 334 }
 335 
 336 /*
 337  * Set extra registers.
 338  *
 339  * We've been given a regset to set. Before we hand it off to the FPU, we have
 340  * to go through and make sure that the different parts of this actually make
 341  * sense. The kernel has guaranteed us through the functions above that we have
 342  * the number of bytes that the header indicates are present. In particular we
 343  * need to validate:
 344  *
 345  *   o The information in the header is reasonable: we have a known type, flags
 346  *     and padding are zero, and there is at least one info structure.
 347  *   o Each of the info structures has a valid type, size, and fits within the
 348  *     data we were given.
 349  *   o We do not validate or modify the actual data in the different pieces for
 350  *     validity. That is considered something that the FPU does. Similarly if
 351  *     something is read-only or not used, that is something that it checks.
 352  *
 353  * While we would like to return something other than EINVAL, the /proc APIs
 354  * pretty much lead that to being the primary errno for all sorts of situations.
 355  */
 356 int
 357 prsetprxregs(klwp_t *lwp, prxregset_t *prx)
 358 {
 359         size_t infosz;
 360         prxregset_hdr_t *hdr = (prxregset_hdr_t *)prx;
 361 
 362         if (hdr->pr_type != PR_TYPE_XSAVE || hdr->pr_flags != 0 ||
 363             hdr->pr_pad[0] != 0 || hdr->pr_pad[1] != 0 || hdr->pr_pad[2] != 0 ||
 364             hdr->pr_pad[3] != 0 || hdr->pr_ninfo == 0) {
 365                 return (EINVAL);
 366         }
 367 
 368         infosz = hdr->pr_ninfo * sizeof (prxregset_info_t) +
 369             sizeof (prxregset_hdr_t);
 370         if (infosz > hdr->pr_size) {
 371                 return (EINVAL);
 372         }
 373 
 374         for (uint32_t i = 0; i < hdr->pr_ninfo; i++) {
 375                 uint32_t exp_size;
 376                 size_t need_len, exp_align;
 377                 const prxregset_info_t *info = &hdr->pr_info[i];
 378 
 379                 switch (info->pri_type) {
 380                 case PRX_INFO_XCR:
 381                         exp_size = sizeof (prxregset_xcr_t);
 382                         exp_align = alignof (prxregset_xcr_t);
 383                         break;
 384                 case PRX_INFO_XSAVE:
 385                         exp_size = sizeof (prxregset_xsave_t);
 386                         exp_align = alignof (prxregset_xsave_t);
 387                         break;
 388                 case PRX_INFO_YMM:
 389                         exp_size = sizeof (prxregset_ymm_t);
 390                         exp_align = alignof (prxregset_ymm_t);
 391                         break;
 392                 case PRX_INFO_OPMASK:
 393                         exp_size = sizeof (prxregset_opmask_t);
 394                         exp_align = alignof (prxregset_opmask_t);
 395                         break;
 396                 case PRX_INFO_ZMM:
 397                         exp_size = sizeof (prxregset_zmm_t);
 398                         exp_align = alignof (prxregset_zmm_t);
 399                         break;
 400                 case PRX_INFO_HI_ZMM:
 401                         exp_size = sizeof (prxregset_hi_zmm_t);
 402                         exp_align = alignof (prxregset_hi_zmm_t);
 403                         break;
 404                 default:
 405                         return (EINVAL);
 406                 }
 407 
 408                 if (info->pri_flags != 0 || info->pri_size != exp_size) {
 409                         return (EINVAL);
 410                 }
 411 
 412                 if ((info->pri_offset % exp_align) != 0) {
 413                         return (EINVAL);
 414                 }
 415 
 416                 /*
 417                  * No bytes of this item's entry should overlap with the
 418                  * information area. If users want to overlap the actual data
 419                  * information for some odd reason, we don't check that and let
 420                  * them do what they want. However, the total data for this
 421                  * region must actually fit. Because exp_size and pri_offset are
 422                  * uint32_t's, we can sum them without overflow worries in an
 423                  * LP64 environment.
 424                  *
 425                  * While we try to grantee alignment when writing this structure
 426                  * out to userland, that is in no way a requirement and users
 427                  * are allowed to start these structures wherever they want.
 428                  * Hence that is not checked here.
 429                  */
 430                 need_len = (size_t)exp_size + (size_t)info->pri_offset;
 431                 if (info->pri_offset < infosz ||
 432                     need_len > (size_t)hdr->pr_size) {
 433                         return (EINVAL);
 434                 }
 435         }
 436 
 437         return (fpu_proc_xregs_set(lwp, prx));
 438 }
 439 
 440 /*
 441  * Return the base (lower limit) of the process stack.
 442  */
 443 caddr_t
 444 prgetstackbase(proc_t *p)
 445 {
 446         return (p->p_usrstack - p->p_stksize);
 447 }
 448 
 449 /*
 450  * Return the "addr" field for pr_addr in prpsinfo_t.
 451  * This is a vestige of the past, so whatever we return is OK.
 452  */
 453 caddr_t
 454 prgetpsaddr(proc_t *p)
 455 {
 456         return ((caddr_t)p);
 457 }
 
 |