Date: Sat, 5 Jun 2010 16:00:53 +0000 (UTC) From: Konstantin Belousov <kib@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r208834 - in head/sys: crypto/via dev/random opencrypto Message-ID: <201006051600.o55G0rfp069153@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: kib Date: Sat Jun 5 16:00:53 2010 New Revision: 208834 URL: http://svn.freebsd.org/changeset/base/208834 Log: 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 Modified: head/sys/crypto/via/padlock.c head/sys/crypto/via/padlock.h head/sys/crypto/via/padlock_cipher.c head/sys/crypto/via/padlock_hash.c head/sys/dev/random/nehemiah.c head/sys/opencrypto/crypto.c Modified: head/sys/crypto/via/padlock.c ============================================================================== --- head/sys/crypto/via/padlock.c Sat Jun 5 15:59:59 2010 (r208833) +++ head/sys/crypto/via/padlock.c Sat Jun 5 16:00:53 2010 (r208834) @@ -169,6 +169,7 @@ padlock_newsession(device_t dev, uint32_ 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_ } 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); Modified: head/sys/crypto/via/padlock.h ============================================================================== --- head/sys/crypto/via/padlock.h Sat Jun 5 15:59:59 2010 (r208833) +++ head/sys/crypto/via/padlock.h Sat Jun 5 16:00:53 2010 (r208834) @@ -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)) Modified: head/sys/crypto/via/padlock_cipher.c ============================================================================== --- head/sys/crypto/via/padlock_cipher.c Sat Jun 5 15:59:59 2010 (r208833) +++ head/sys/crypto/via/padlock_cipher.c Sat Jun 5 16:00:53 2010 (r208834) @@ -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_se 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_se 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_se AES_BLOCK_LEN, ses->ses_iv); } + out: if (allocated) { bzero(buf, enccrd->crd_len + 16); free(buf, M_PADLOCK); } - return (0); + return (error); } Modified: head/sys/crypto/via/padlock_hash.c ============================================================================== --- head/sys/crypto/via/padlock_hash.c Sat Jun 5 15:59:59 2010 (r208833) +++ head/sys/crypto/via/padlock_hash.c Sat Jun 5 16:00:53 2010 (r208834) @@ -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); } Modified: head/sys/dev/random/nehemiah.c ============================================================================== --- head/sys/dev/random/nehemiah.c Sat Jun 5 15:59:59 2010 (r208833) +++ head/sys/dev/random/nehemiah.c Sat Jun 5 16:00:53 2010 (r208834) @@ -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] 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); } Modified: head/sys/opencrypto/crypto.c ============================================================================== --- head/sys/opencrypto/crypto.c Sat Jun 5 15:59:59 2010 (r208833) +++ head/sys/opencrypto/crypto.c Sat Jun 5 16:00:53 2010 (r208834) @@ -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 (;;) { /*
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201006051600.o55G0rfp069153>