10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7425SGongtian.Zhao@Sun.COM * Common Development and Distribution License (the "License").
6*7425SGongtian.Zhao@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*7425SGongtian.Zhao@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate * rseq implementation
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include <sys/usb/clients/usbser/usbser_rseq.h>
320Sstevel@tonic-gate
330Sstevel@tonic-gate #ifdef _KERNEL
340Sstevel@tonic-gate #include <sys/debug.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate static long rseq_random();
380Sstevel@tonic-gate #define random rseq_random
390Sstevel@tonic-gate #else
400Sstevel@tonic-gate #include <assert.h>
410Sstevel@tonic-gate #define ASSERT assert
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #endif
440Sstevel@tonic-gate
450Sstevel@tonic-gate
460Sstevel@tonic-gate /*ARGSUSED*/
470Sstevel@tonic-gate static int
rseq_do_common(rseq_t * rseq,int num,uintptr_t arg,int flags,int fail_err,uintptr_t fail_num)480Sstevel@tonic-gate rseq_do_common(rseq_t *rseq, int num, uintptr_t arg, int flags, int fail_err,
490Sstevel@tonic-gate uintptr_t fail_num)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate int i;
520Sstevel@tonic-gate rseq_step_t *s;
530Sstevel@tonic-gate int rval = RSEQ_OK;
540Sstevel@tonic-gate
550Sstevel@tonic-gate for (i = 0; i < num; i++) {
560Sstevel@tonic-gate s = &rseq[i].r_do;
570Sstevel@tonic-gate
580Sstevel@tonic-gate if (s->s_func == NULL) {
590Sstevel@tonic-gate continue;
600Sstevel@tonic-gate }
610Sstevel@tonic-gate s->s_rval = (i != fail_num) ? s->s_func(arg) : fail_err;
620Sstevel@tonic-gate rval = (s->s_cb) ? (s->s_cb(rseq, i, arg)) : RSEQ_OK;
630Sstevel@tonic-gate
640Sstevel@tonic-gate if (rval == RSEQ_UNDO) {
650Sstevel@tonic-gate (void) rseq_undo(rseq, i, arg, flags);
660Sstevel@tonic-gate break;
670Sstevel@tonic-gate } else if (rval == RSEQ_ABORT) {
680Sstevel@tonic-gate break;
690Sstevel@tonic-gate }
700Sstevel@tonic-gate ASSERT(rval == RSEQ_OK);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate return (rval);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate
750Sstevel@tonic-gate
760Sstevel@tonic-gate /*ARGSUSED*/
770Sstevel@tonic-gate static int
rseq_undo_common(rseq_t * rseq,int num,uintptr_t arg,int flags,int fail_err,uintptr_t fail_num)780Sstevel@tonic-gate rseq_undo_common(rseq_t *rseq, int num, uintptr_t arg, int flags, int fail_err,
790Sstevel@tonic-gate uintptr_t fail_num)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate int i;
820Sstevel@tonic-gate rseq_step_t *s;
830Sstevel@tonic-gate int rval = RSEQ_OK;
840Sstevel@tonic-gate
850Sstevel@tonic-gate for (i = num - 1; i >= 0; i--) {
860Sstevel@tonic-gate s = &rseq[i].r_undo;
870Sstevel@tonic-gate
880Sstevel@tonic-gate if (s->s_func == NULL) {
890Sstevel@tonic-gate continue;
900Sstevel@tonic-gate }
910Sstevel@tonic-gate s->s_rval = (i != fail_num) ? s->s_func(arg) : fail_err;
920Sstevel@tonic-gate rval = (s->s_cb) ? (s->s_cb(rseq, i, arg)) : RSEQ_OK;
930Sstevel@tonic-gate
940Sstevel@tonic-gate if (rval == RSEQ_ABORT) {
950Sstevel@tonic-gate break;
960Sstevel@tonic-gate }
970Sstevel@tonic-gate ASSERT(rval == RSEQ_OK);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate return (rval);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate int
rseq_do(rseq_t * rseq,int num,uintptr_t arg,int flags)1040Sstevel@tonic-gate rseq_do(rseq_t *rseq, int num, uintptr_t arg, int flags)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate return (rseq_do_common(rseq, num, arg, flags, 0, -1));
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate int
rseq_undo(rseq_t * rseq,int num,uintptr_t arg,int flags)1110Sstevel@tonic-gate rseq_undo(rseq_t *rseq, int num, uintptr_t arg, int flags)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate return (rseq_undo_common(rseq, num, arg, flags, 0, -1));
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate #ifdef DEBUG
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate #ifndef __lock_lint
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate static int
rseq_debug(rseq_t * rseq,int num,uintptr_t arg,int flags,int scenario,uintptr_t sarg1,uintptr_t sarg2,int (* func)(rseq_t *,int,uintptr_t,int,int,uintptr_t))1210Sstevel@tonic-gate rseq_debug(rseq_t *rseq, int num, uintptr_t arg, int flags, int scenario,
1220Sstevel@tonic-gate uintptr_t sarg1, uintptr_t sarg2,
1230Sstevel@tonic-gate int (*func)(rseq_t *, int, uintptr_t, int, int, uintptr_t))
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate int rnd, rval, i;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate switch (scenario) {
1280Sstevel@tonic-gate case RSEQ_DBG_FAIL_ONE:
1290Sstevel@tonic-gate rval = func(rseq, num, arg, flags, sarg1, sarg2);
1300Sstevel@tonic-gate break;
1310Sstevel@tonic-gate case RSEQ_DBG_FAIL_ONE_RANDOM:
1320Sstevel@tonic-gate rnd = random() % num;
1330Sstevel@tonic-gate rval = func(rseq, num, arg, flags, sarg1, rnd);
1340Sstevel@tonic-gate break;
1350Sstevel@tonic-gate case RSEQ_DBG_FAIL_ONEBYONE:
1360Sstevel@tonic-gate for (i = 0; i < num; i++) {
1370Sstevel@tonic-gate rval = func(rseq, num, arg, flags, sarg1, i);
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate * when aborted, the undo path is not executed, so we
1400Sstevel@tonic-gate * can't continue without the risk of resource leakage.
1410Sstevel@tonic-gate */
1420Sstevel@tonic-gate if (rval == RSEQ_ABORT) {
1430Sstevel@tonic-gate break;
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate break;
1470Sstevel@tonic-gate default:
1480Sstevel@tonic-gate ASSERT(!"rseq_debug: incorrect debug scenario");
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate return (rval);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate int
rseq_do_debug(rseq_t * rseq,int num,uintptr_t arg,int flags,int scenario,uintptr_t sarg1,uintptr_t sarg2)1550Sstevel@tonic-gate rseq_do_debug(rseq_t *rseq, int num, uintptr_t arg, int flags, int scenario,
1560Sstevel@tonic-gate uintptr_t sarg1, uintptr_t sarg2)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate return (rseq_debug(rseq, num, arg, flags, scenario, sarg1, sarg2,
159*7425SGongtian.Zhao@Sun.COM rseq_do_common));
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate int
rseq_undo_debug(rseq_t * rseq,int num,uintptr_t arg,int flags,int scenario,uintptr_t sarg1,uintptr_t sarg2)1640Sstevel@tonic-gate rseq_undo_debug(rseq_t *rseq, int num, uintptr_t arg, int flags, int scenario,
1650Sstevel@tonic-gate uintptr_t sarg1, uintptr_t sarg2)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate return (rseq_debug(rseq, num, arg, flags, scenario, sarg1, sarg2,
168*7425SGongtian.Zhao@Sun.COM rseq_undo_common));
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate #ifdef _KERNEL
1720Sstevel@tonic-gate static long
rseq_random()1730Sstevel@tonic-gate rseq_random()
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate return (ddi_get_lbolt());
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate #endif /* _KERNEL */
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate #endif /* __lock_lint */
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate #endif /* DEBUG */
182