Use the fpu_kern_enter() interface to properly separate usermode FPU

context from in-kernel execution of padlock instructions and to handle
spurious FPUDNA exceptions that sometime are raised when doing padlock
calculations.

Globally mark crypto(9) kthread as using FPU.

Reviewed by:	pjd
Hardware provided by:	Sentex Communications
Tested by:	  pho
PR:    amd64/135014
MFC after:    1 month
This commit is contained in:
Konstantin Belousov 2010-06-05 16:00:53 +00:00
parent 6cf9a08d2c
commit 04c49e68de
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=208834
6 changed files with 53 additions and 4 deletions

View file

@ -169,6 +169,7 @@ padlock_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri)
struct padlock_softc *sc = device_get_softc(dev);
struct padlock_session *ses = NULL;
struct cryptoini *encini, *macini;
struct thread *td;
int error;
if (sidp == NULL || cri == NULL)
@ -236,7 +237,12 @@ padlock_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri)
}
if (macini != NULL) {
error = padlock_hash_setup(ses, macini);
td = curthread;
error = fpu_kern_enter(td, &ses->ses_fpu_ctx, FPU_KERN_NORMAL);
if (error == 0) {
error = padlock_hash_setup(ses, macini);
fpu_kern_leave(td, &ses->ses_fpu_ctx);
}
if (error != 0) {
padlock_freesession_one(sc, ses, 0);
return (error);

View file

@ -32,6 +32,12 @@
#include <opencrypto/cryptodev.h>
#include <crypto/rijndael/rijndael.h>
#if defined(__i386__)
#include <machine/npx.h>
#elif defined(__amd64__)
#include <machine/fpu.h>
#endif
union padlock_cw {
uint64_t raw;
struct {
@ -70,6 +76,7 @@ struct padlock_session {
int ses_used;
uint32_t ses_id;
TAILQ_ENTRY(padlock_session) ses_next;
struct fpu_kern_ctx ses_fpu_ctx;
};
#define PADLOCK_ALIGN(p) (void *)(roundup2((uintptr_t)(p), 16))

View file

@ -53,6 +53,7 @@ __FBSDID("$FreeBSD$");
#include <sys/module.h>
#include <sys/malloc.h>
#include <sys/libkern.h>
#include <sys/pcpu.h>
#include <sys/uio.h>
#include <opencrypto/cryptodev.h>
@ -201,9 +202,10 @@ padlock_cipher_process(struct padlock_session *ses, struct cryptodesc *enccrd,
struct cryptop *crp)
{
union padlock_cw *cw;
struct thread *td;
u_char *buf, *abuf;
uint32_t *key;
int allocated;
int allocated, error;
buf = padlock_cipher_alloc(enccrd, crp, &allocated);
if (buf == NULL)
@ -247,9 +249,16 @@ padlock_cipher_process(struct padlock_session *ses, struct cryptodesc *enccrd,
enccrd->crd_len, abuf);
}
td = curthread;
error = fpu_kern_enter(td, &ses->ses_fpu_ctx, FPU_KERN_NORMAL);
if (error != 0)
goto out;
padlock_cbc(abuf, abuf, enccrd->crd_len / AES_BLOCK_LEN, key, cw,
ses->ses_iv);
fpu_kern_leave(td, &ses->ses_fpu_ctx);
if (allocated) {
crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
enccrd->crd_len, abuf);
@ -262,9 +271,10 @@ padlock_cipher_process(struct padlock_session *ses, struct cryptodesc *enccrd,
AES_BLOCK_LEN, ses->ses_iv);
}
out:
if (allocated) {
bzero(buf, enccrd->crd_len + 16);
free(buf, M_PADLOCK);
}
return (0);
return (error);
}

View file

@ -34,12 +34,14 @@ __FBSDID("$FreeBSD$");
#include <sys/malloc.h>
#include <sys/libkern.h>
#include <sys/endian.h>
#include <sys/pcpu.h>
#if defined(__amd64__) || (defined(__i386__) && !defined(PC98))
#include <machine/cpufunc.h>
#include <machine/cputypes.h>
#include <machine/md_var.h>
#include <machine/specialreg.h>
#endif
#include <machine/pcb.h>
#include <opencrypto/cryptodev.h>
#include <opencrypto/cryptosoft.h> /* for hmac_ipad_buffer and hmac_opad_buffer */
@ -363,12 +365,18 @@ int
padlock_hash_process(struct padlock_session *ses, struct cryptodesc *maccrd,
struct cryptop *crp)
{
struct thread *td;
int error;
td = curthread;
error = fpu_kern_enter(td, &ses->ses_fpu_ctx, FPU_KERN_NORMAL);
if (error != 0)
return (error);
if ((maccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0)
padlock_hash_key_setup(ses, maccrd->crd_key, maccrd->crd_klen);
error = padlock_authcompute(ses, maccrd, crp->crp_buf, crp->crp_flags);
fpu_kern_leave(td, &ses->ses_fpu_ctx);
return (error);
}

View file

@ -35,6 +35,8 @@ __FBSDID("$FreeBSD$");
#include <sys/selinfo.h>
#include <sys/systm.h>
#include <machine/pcb.h>
#include <dev/random/randomdev.h>
#define RANDOM_BLOCK_SIZE 256
@ -82,6 +84,8 @@ static uint8_t out[RANDOM_BLOCK_SIZE+7] __aligned(16);
static union VIA_ACE_CW acw __aligned(16);
static struct fpu_kern_ctx fpu_ctx_save;
static struct mtx random_nehemiah_mtx;
/* ARGSUSED */
@ -142,11 +146,16 @@ random_nehemiah_deinit(void)
static int
random_nehemiah_read(void *buf, int c)
{
int i;
int i, error;
size_t count, ret;
uint8_t *p;
mtx_lock(&random_nehemiah_mtx);
error = fpu_kern_enter(curthread, &fpu_ctx_save, FPU_KERN_NORMAL);
if (error != 0) {
mtx_unlock(&random_nehemiah_mtx);
return (0);
}
/* Get a random AES key */
count = 0;
@ -187,6 +196,7 @@ random_nehemiah_read(void *buf, int c)
c = MIN(RANDOM_BLOCK_SIZE, c);
memcpy(buf, out, (size_t)c);
fpu_kern_leave(curthread, &fpu_ctx_save);
mtx_unlock(&random_nehemiah_mtx);
return (c);
}

View file

@ -82,6 +82,10 @@ __FBSDID("$FreeBSD$");
#include <sys/bus.h>
#include "cryptodev_if.h"
#if defined(__i386__) || defined(__amd64__)
#include <machine/pcb.h>
#endif
SDT_PROVIDER_DEFINE(opencrypto);
/*
@ -1241,6 +1245,10 @@ crypto_proc(void)
u_int32_t hid;
int result, hint;
#if defined(__i386__) || defined(__amd64__)
fpu_kern_thread(FPU_KERN_NORMAL);
#endif
CRYPTO_Q_LOCK();
for (;;) {
/*