* ========== Copyright Header Begin ==========================================
* Hypervisor Software File: rng.s
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* - Do no alter or remove copyright notices
* - Redistribution and use of this software in source and binary forms, with
* or without modification, are permitted provided that the following
* - Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of Sun Microsystems, Inc. or the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* This software is provided "AS IS," without a warranty of any kind.
* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
* MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
* OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
* FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
* DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
* SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* You acknowledge that this software is not designed, licensed or
* intended for use in the design, construction, operation or maintenance of
* ========== Copyright Header End ============================================
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
.ident "@(#)rng.s 1.2 07/07/19 SMI"
#include <sys/asm_linkage.h>
* Delay necessary because of how CTL bits are serially
#define CTL_REG_DELAY_CYCLES 64
#define CTL_REG_DELAY(scr) \
mov CTL_REG_DELAY_CYCLES, scr ; \
* Locking primitives for RNG_LOCK
#define RNG_TRYLOCK(rng, scr, lck) \
add rng, RNG_LOCK, scr ; \
casa [scr]ASI_P, %g0, lck
#define RNG_UNLOCK(rng) \
*-----------------------------------------------------------
* Function: rng_get_diag_control()
* %o0 - EOK (on success),
* EWOULDBLOCK, ENOACCESS (on failure)
*-----------------------------------------------------------
ENTRY_NP(hcall_rng_get_diag_control)
* Check that caller has CTL access.
setx GUEST_RNG_CTL_ACCESSIBLE, %g5, %g2
brz,a,pn %g2, herr_noaccess
ldx [%g4 + CONFIG_RNG], %g1
RNG_TRYLOCK(%g1, %g5, %g2)
brnz,pn %g2, herr_wouldblock
ldx [%g3 + GUEST_GID], %g3
stx %g3, [%g1 + RNG_CTL + RNG_CTLDATA_GUESTID]
SET_SIZE(hcall_rng_get_diag_control)
*-----------------------------------------------------------
* Function: rng_ctl_read(struct rng_ctlregs rctlptr)
* %o0 - struct rng_ctlregs pointer
* %o0 - EOK (on success),
* EWOULDBLOCK, ENOACCESS, EBADALIGN, ENORADDR (on failure)
* %o2 - Ready delta (system ticks)
*-----------------------------------------------------------
ENTRY_NP(hcall_rng_ctl_read)
bnz,pn %xcc, herr_badalign
* Check that caller has CTL access.
setx GUEST_RNG_CTL_ACCESSIBLE, %g5, %g4
brz,pn %g4, herr_noaccess
ldx [%g4 + CONFIG_RNG], %g1
mov RNG_CTLREGS_SIZE, %g2
RA2PA_RANGE_CONV_UNK_SIZE(%g3, %o0, %g2, herr_noraddr, %g5, %g4)
RNG_TRYLOCK(%g1, %g5, %g2)
brnz,a,pn %g2, herr_wouldblock
ldx [%g1 + RNG_CTL + RNG_CTLDATA_STATE], %o1
ldx [%g1 + RNG_CTL + RNG_CTLDATA_READYTIME], %g3
rd STICK, %g5 ! current time
movgeu %xcc, %g0, %o2 ! delta
ldx [%g1 + RNG_CTL + RNG_CTLDATA_REGS + RNG_CTLREGS_REG0], %g3
stx %g3, [%g4 + RNG_CTLREGS_REG0]
ldx [%g1 + RNG_CTL + RNG_CTLDATA_REGS + RNG_CTLREGS_REG1], %g3
stx %g3, [%g4 + RNG_CTLREGS_REG1]
ldx [%g1 + RNG_CTL + RNG_CTLDATA_REGS + RNG_CTLREGS_REG2], %g3
stx %g3, [%g4 + RNG_CTLREGS_REG2]
ldx [%g1 + RNG_CTL + RNG_CTLDATA_REGS + RNG_CTLREGS_REG3], %g3
stx %g3, [%g4 + RNG_CTLREGS_REG3]
SET_SIZE(hcall_rng_ctl_read)
*-----------------------------------------------------------
* Function: rng_ctl_write(struct rng_ctlregs rctlptr, uint64_t nstate,
* %o0 - struct rng_ctlregs pointer
* %o2 - Watchdog timeout (system ticks)
* %o0 - EOK (on success),
* EWOULDBLOCK, ENOACCESS, EBADALIGN, ENORADDR,
* EIO, EINVAL (on failure)
* %o1 - Ready delta (system ticks)
*-----------------------------------------------------------
ENTRY_NP(hcall_rng_ctl_write)
bnz,pn %xcc, herr_badalign
* Check that caller has diagnostic access & control.
setx GUEST_RNG_CTL_ACCESSIBLE, %g5, %g4
brz,pn %g4, herr_noaccess
mov RNG_CTLREGS_SIZE, %g2
RA2PA_RANGE_CONV_UNK_SIZE(%g3, %o0, %g2, herr_noraddr, %g5, %g7)
ldx [%g4 + CONFIG_RNG], %g1
RNG_TRYLOCK(%g1, %g5, %g2)
brnz,a,pn %g2, herr_wouldblock
ldx [%g3 + GUEST_GID], %g5
ldx [%g1 + RNG_CTL + RNG_CTLDATA_GUESTID], %g4
cmp %o1, RNG_STATE_CONFIGURED
cmp %o1, RNG_STATE_HEALTHCHECK
cmp %o1, RNG_STATE_UNCONFIGURED
* newstate = UNCONFIGURED or ERROR.
stx %o1, [%g1 + RNG_CTL + RNG_CTLDATA_STATE]
ldx [%g1 + RNG_CTL + RNG_CTLDATA_READYTIME], %g4
rd STICK, %g5 ! current time
* newstate = CONFIGURED or HEALTHCHECK.
* Verify that CTL is ready to be changed.
ldx [%g1 + RNG_CTL + RNG_CTLDATA_READYTIME], %g4
rd STICK, %g5 ! current time
stx %g5, [%g1 + RNG_CTL + RNG_CTLDATA_READYTIME]
setx RNG_CTL_MASK, %g6, %g2
* Check callers CTLREG values and make sure they are
ldx [%o0 + RNG_CTLREGS_REG0], %g3
ldx [%o0 + RNG_CTLREGS_REG1], %g4
ldx [%o0 + RNG_CTLREGS_REG2], %g5
ldx [%o0 + RNG_CTLREGS_REG3], %g6
* Values are valid. Save a copy off in our CTLDATA.
stx %g3, [%g1 + RNG_CTL + RNG_CTLDATA_REGS + RNG_CTLREGS_REG0]
stx %g4, [%g1 + RNG_CTL + RNG_CTLDATA_REGS + RNG_CTLREGS_REG1]
stx %g5, [%g1 + RNG_CTL + RNG_CTLDATA_REGS + RNG_CTLREGS_REG2]
stx %g6, [%g1 + RNG_CTL + RNG_CTLDATA_REGS + RNG_CTLREGS_REG3]
* Store values into RNG.CTL.
* The RNG hardware control register is funky. You store
* to different control registers by specifying different
* bits in the value being stored, but all stores happen
* against the same RNG.CTL address.
setx RNG_CTL_ADDR, %o0, %g2
* If caller specified a watchdog timeout value of 0,
* then that means no watchdog necessary. If caller
* specified negative value then overwrite to 0.
stx %g5, [%g1 + RNG_CTL + RNG_CTLDATA_READYTIME]
stx %o1, [%g1 + RNG_CTL + RNG_CTLDATA_STATE]
SET_SIZE(hcall_rng_ctl_write)
*-----------------------------------------------------------
* Function: rng_data_read_diag(uint64_t addr, uint64_t sz)
* %o0 - Address of data buffer
* %o1 - Size of data buffer (bytes)
* %o0 - EOK (on success),
* EWOULDBLOCK, ENOACCESS, EBADALIGN, ENORADDR,
* EIO, EINVAL (on failure)
* %o1 - Ready delta (system ticks)
*-----------------------------------------------------------
ENTRY_NP(hcall_rng_data_read_diag)
bnz,pn %xcc, herr_badalign
* Check that caller has diagnostic access & control.
setx GUEST_RNG_CTL_ACCESSIBLE, %g5, %g4
brz,pn %g4, herr_noaccess
* Verify size falls within desired range:
* RNG_DATA_MINLEN...RNG_DATA_MAXLEN
bnz,pn %xcc, herr_badalign
setx RNG_DATA_MAXLEN, %g5, %g4
RA2PA_RANGE_CONV_UNK_SIZE(%g3, %o0, %o1, herr_noraddr, %g2, %g7)
ldx [%g4 + CONFIG_RNG], %g1
RNG_TRYLOCK(%g1, %g5, %g2)
brnz,a,pn %g2, herr_wouldblock
ldx [%g3 + GUEST_GID], %g5
ldx [%g1 + RNG_CTL + RNG_CTLDATA_GUESTID], %g4
* Check that the RNG is ready,
* i.e. STICK >= ctldata.readytime.
ldx [%g1 + RNG_CTL + RNG_CTLDATA_READYTIME], %g4
rd STICK, %g5 ! current time
stx %g5, [%g1 + RNG_CTL + RNG_CTLDATA_READYTIME]
setx RNG_DATA_ADDR, %g3, %g4
ldx [%g4], %g3 ! read RNG.DATA
SET_SIZE(hcall_rng_data_read_diag)
*-----------------------------------------------------------
* Function: rng_data_read(uint64_t addr)
* %o0 - Address of data buffer (size = 8 bytes)
* %o0 - EOK (on success),
* EWOULDBLOCK, ENOACCESS, EBADALIGN, ENORADDR,
* %o1 - Ready delta (system ticks)
*-----------------------------------------------------------
ENTRY_NP(hcall_rng_data_read)
bnz,pn %xcc, herr_badalign
RA2PA_RANGE_CONV_UNK_SIZE(%g3, %o0, %g5, herr_noraddr, %g2, %g7)
!! %o0 = physical address of buffer
ldx [%g4 + CONFIG_RNG], %g1
RNG_TRYLOCK(%g1, %g5, %g2)
brnz,pn %g2, herr_wouldblock
mov %g0, %o1 ! *tdelta = 0
ldx [%g1 + RNG_CTL + RNG_CTLDATA_STATE], %g4
cmp %g4, RNG_STATE_CONFIGURED
ldx [%g1 + RNG_CTL + RNG_CTLDATA_READYTIME], %g2
move %xcc, ENOACCESS, %g6
rd STICK, %g3 ! current time
stx %g3, [%g1 + RNG_CTL + RNG_CTLDATA_READYTIME]
setx RNG_DATA_ADDR, %g3, %g4
ldx [%g4], %g3 ! read RNG.DATA
SET_SIZE(hcall_rng_data_read)