dm: vpit: clean up asserts

Remove the use of assert() in vPIT.

Tracked-On: #3252
Signed-off-by: Peter Fang <peter.fang@intel.com>
Reviewed-by: <yonghua.huang@intel.com>
Acked-by: Yin, Fengwei <fengwei.yin@intel.com>
This commit is contained in:
Peter Fang 2019-06-28 21:52:29 -07:00 committed by wenlingz
parent 81f9837e8a
commit aac8275098

View File

@ -27,11 +27,13 @@
*/ */
#include <pthread.h> #include <pthread.h>
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <err.h>
#include <sysexits.h>
#include <string.h>
#include "vmmapi.h" #include "vmmapi.h"
#include "timer.h" #include "timer.h"
@ -46,18 +48,22 @@
#define PERIODIC_MODE(mode) \ #define PERIODIC_MODE(mode) \
((mode) == TIMER_RATEGEN || (mode) == TIMER_SQWAVE) ((mode) == TIMER_RATEGEN || (mode) == TIMER_SQWAVE)
#define VPIT_LOCK() \ #define VPIT_LOCK() \
do { \ do { \
int err; \ int err; \
err = pthread_mutex_lock(&vpit_mtx); \ err = pthread_mutex_lock(&vpit_mtx); \
assert(err == 0); \ if (err) \
errx(EX_SOFTWARE, "pthread_mutex_lock returned %s", \
strerror(err)); \
} while (0) } while (0)
#define VPIT_UNLOCK() \ #define VPIT_UNLOCK() \
do { \ do { \
int err; \ int err; \
err = pthread_mutex_unlock(&vpit_mtx); \ err = pthread_mutex_unlock(&vpit_mtx); \
assert(err == 0); \ if (err) \
errx(EX_SOFTWARE, "pthread_mutex_unlock returned %s", \
strerror(err)); \
} while (0) } while (0)
#define vpit_ts_to_ticks(ts) ts_to_ticks(PIT_8254_FREQ, ts) #define vpit_ts_to_ticks(ts) ts_to_ticks(PIT_8254_FREQ, ts)
@ -101,14 +107,12 @@ static uint64_t
ticks_elapsed_since(const struct timespec *since) ticks_elapsed_since(const struct timespec *since)
{ {
struct timespec ts; struct timespec ts;
int error;
error = clock_gettime(CLOCK_REALTIME, &ts); if (clock_gettime(CLOCK_REALTIME, &ts))
assert(error == 0); errx(EX_SOFTWARE, "clock_gettime returned: %s", strerror(errno));
if (timespeccmp(&ts, since, <=)) { if (timespeccmp(&ts, since, <=))
return 0; return 0;
}
timespecsub(&ts, since); timespecsub(&ts, since);
return vpit_ts_to_ticks(&ts); return vpit_ts_to_ticks(&ts);
@ -159,9 +163,7 @@ vpit_get_out(struct vpit *vpit, int channel, uint64_t delta_ticks)
out = (initval) ? 1 : (delta_ticks != c->initial); out = (initval) ? 1 : (delta_ticks != c->initial);
break; break;
default: default:
printf("vpit invalid timer mode: %d\n", c->mode); errx(EX_SOFTWARE, "vpit invalid timer mode: %d", c->mode);
assert(0);
break;
} }
return out; return out;
@ -185,16 +187,20 @@ pit_cr_val(uint8_t cr[2])
static void static void
pit_load_ce(struct channel *c) pit_load_ce(struct channel *c)
{ {
int error;
/* no CR update in progress */ /* no CR update in progress */
if (c->nullcnt && c->crbyte == 2) { if (c->nullcnt && c->crbyte == 2) {
c->initial = pit_cr_val(c->cr); c->initial = pit_cr_val(c->cr);
c->nullcnt = false; c->nullcnt = false;
c->crbyte = 0; c->crbyte = 0;
error = clock_gettime(CLOCK_REALTIME, &c->start_ts);
assert(error == 0); if (clock_gettime(CLOCK_REALTIME, &c->start_ts))
assert(c->initial > 0 && c->initial <= 0x10000); errx(EX_SOFTWARE, "clock_gettime returned: %s", strerror(errno));
if (c->initial == 0 || c->initial > 0x10000) {
warnx("vpit invalid initial count: 0x%x - use 0x10000",
c->initial);
c->initial = 0x10000;
}
} }
} }
@ -220,7 +226,12 @@ vpit_timer_handler(union sigval s)
/* it's now safe to use the vpit pointer */ /* it's now safe to use the vpit pointer */
vpit = arg->vpit; vpit = arg->vpit;
assert(vpit != NULL);
if (vpit == NULL) {
warnx("vpit is NULL");
goto done;
}
c = &vpit->channel[arg->channel_num]; c = &vpit->channel[arg->channel_num];
/* generate a rising edge on OUT */ /* generate a rising edge on OUT */
@ -238,7 +249,6 @@ pit_timer_stop_cntr0(struct vpit *vpit, struct itimerspec *rem)
{ {
struct channel *c; struct channel *c;
bool active; bool active;
int error;
c = &vpit->channel[0]; c = &vpit->channel[0];
active = pit_cntr0_timer_running(vpit); active = pit_cntr0_timer_running(vpit);
@ -247,18 +257,21 @@ pit_timer_stop_cntr0(struct vpit *vpit, struct itimerspec *rem)
vpit_timer_arg[c->timer_idx].active = false; vpit_timer_arg[c->timer_idx].active = false;
if (rem) { if (rem) {
error = timer_gettime(c->timer_id, rem); if (timer_gettime(c->timer_id, rem))
assert(error == 0); errx(EX_SOFTWARE,
"timer_gettime returned: %s", strerror(errno));
} }
error = timer_delete(c->timer_id); if (timer_delete(c->timer_id))
assert(error == 0); errx(EX_SOFTWARE, "timer_delete returned: %s", strerror(errno));
if (++c->timer_idx == nitems(vpit_timer_arg)) { if (++c->timer_idx == nitems(vpit_timer_arg))
c->timer_idx = 0; c->timer_idx = 0;
}
assert(!pit_cntr0_timer_running(vpit)); if (pit_cntr0_timer_running(vpit)) {
warnx("vpit timer %d is still active", c->timer_idx);
vpit_timer_arg[c->timer_idx].active = false;
}
} }
return active; return active;
@ -267,7 +280,6 @@ pit_timer_stop_cntr0(struct vpit *vpit, struct itimerspec *rem)
static void static void
pit_timer_start_cntr0(struct vpit *vpit) pit_timer_start_cntr0(struct vpit *vpit)
{ {
int error;
struct channel *c; struct channel *c;
struct itimerspec ts = { 0 }; struct itimerspec ts = { 0 };
struct sigevent sigevt = { 0 }; struct sigevent sigevt = { 0 };
@ -289,7 +301,8 @@ pit_timer_start_cntr0(struct vpit *vpit)
* always updated in the second half-cycle (before a rising * always updated in the second half-cycle (before a rising
* edge on OUT). * edge on OUT).
*/ */
assert(timespecisset(&ts.it_interval)); if (!timespecisset(&ts.it_interval))
warnx("vpit is in periodic mode but with a one-shot timer");
/* ts.it_value contains the remaining time until expiration */ /* ts.it_value contains the remaining time until expiration */
vpit_ticks_to_ts(pit_cr_val(c->cr), &ts.it_interval); vpit_ticks_to_ts(pit_cr_val(c->cr), &ts.it_interval);
@ -307,10 +320,11 @@ pit_timer_start_cntr0(struct vpit *vpit)
vpit_ticks_to_ts(timer_ticks, &ts.it_value); vpit_ticks_to_ts(timer_ticks, &ts.it_value);
/* make it periodic if required */ /* make it periodic if required */
if (PERIODIC_MODE(c->mode)) { if (PERIODIC_MODE(c->mode))
ts.it_interval = ts.it_value; ts.it_interval = ts.it_value;
} else { else if (timespecisset(&ts.it_interval)) {
assert(!timespecisset(&ts.it_interval)); warnx("vpit is in aperiodic mode but with a periodic timer");
memset(&ts.it_interval, 0, sizeof(ts.it_interval));
} }
} }
@ -318,22 +332,20 @@ pit_timer_start_cntr0(struct vpit *vpit)
sigevt.sigev_notify = SIGEV_THREAD; sigevt.sigev_notify = SIGEV_THREAD;
sigevt.sigev_notify_function = vpit_timer_handler; sigevt.sigev_notify_function = vpit_timer_handler;
error = timer_create(CLOCK_REALTIME, &sigevt, &c->timer_id); if (timer_create(CLOCK_REALTIME, &sigevt, &c->timer_id))
assert(error == 0); errx(EX_SOFTWARE, "timer_create returned: %s", strerror(errno));
assert(!pit_cntr0_timer_running(vpit));
vpit_timer_arg[c->timer_idx].active = true; vpit_timer_arg[c->timer_idx].active = true;
/* arm the timer */ /* arm the timer */
error = timer_settime(c->timer_id, 0, &ts, NULL); if (timer_settime(c->timer_id, 0, &ts, NULL))
assert(error == 0); errx(EX_SOFTWARE, "timer_settime returned: %s", strerror(errno));
} }
static uint16_t static uint16_t
pit_update_counter(struct vpit *vpit, struct channel *c, bool latch, pit_update_counter(struct vpit *vpit, struct channel *c, bool latch,
uint64_t *ticks_elapsed) uint64_t *ticks_elapsed)
{ {
int error;
uint16_t lval = 0; uint16_t lval = 0;
uint64_t delta_ticks; uint64_t delta_ticks;
@ -350,8 +362,8 @@ pit_update_counter(struct vpit *vpit, struct channel *c, bool latch,
c->initial = PIT_HZ_TO_TICKS(100); c->initial = PIT_HZ_TO_TICKS(100);
delta_ticks = 0; delta_ticks = 0;
error = clock_gettime(CLOCK_REALTIME, &c->start_ts); if (clock_gettime(CLOCK_REALTIME, &c->start_ts))
assert(error == 0); errx(EX_SOFTWARE, "clock_gettime returned: %s", strerror(errno));
} else } else
delta_ticks = ticks_elapsed_since(&c->start_ts); delta_ticks = ticks_elapsed_since(&c->start_ts);
@ -373,9 +385,7 @@ pit_update_counter(struct vpit *vpit, struct channel *c, bool latch,
break; break;
} }
default: default:
printf("vpit invalid timer mode: %d\n", c->mode); errx(EX_SOFTWARE, "vpit invalid timer mode: %d", c->mode);
assert(0);
break;
} }
/* cannot latch a new value until the old one has been consumed */ /* cannot latch a new value until the old one has been consumed */
@ -512,14 +522,17 @@ vpit_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
int error = 0; int error = 0;
if (bytes != 1) { if (bytes != 1) {
printf("vpit invalid operation size: %d bytes\n", bytes); warnx("vpit invalid operation size: %d bytes", bytes);
return (-1); return -1;
} }
val = *eax; val = *eax;
if (port == TIMER_MODE) { if (port == TIMER_MODE) {
assert(!in); if (in) {
warnx("invalid in op @ io port 0x%x", port);
return -1;
}
VPIT_LOCK(); VPIT_LOCK();
error = vpit_update_mode(vpit, val); error = vpit_update_mode(vpit, val);
@ -529,7 +542,11 @@ vpit_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
} }
/* counter ports */ /* counter ports */
assert(port >= TIMER_CNTR0 && port <= TIMER_CNTR2); if (port < TIMER_CNTR0 || port > TIMER_CNTR2) {
warnx("invalid %s op @ io port 0x%x", in ? "in" : "out", port);
return -1;
}
c = &vpit->channel[port - TIMER_CNTR0]; c = &vpit->channel[port - TIMER_CNTR0];
VPIT_LOCK(); VPIT_LOCK();
@ -678,31 +695,23 @@ void
vpit_deinit(struct vmctx *ctx) vpit_deinit(struct vmctx *ctx)
{ {
struct vpit *vpit; struct vpit *vpit;
int i;
VPIT_LOCK(); VPIT_LOCK();
vpit = ctx->vpit; vpit = ctx->vpit;
if (vpit == NULL) { if (vpit == NULL)
goto done; goto done;
}
ctx->vpit = NULL; ctx->vpit = NULL;
pit_timer_stop_cntr0(vpit, NULL); pit_timer_stop_cntr0(vpit, NULL);
memset(vpit_timer_arg, 0, sizeof(vpit_timer_arg));
for (i = 0; i < nitems(vpit_timer_arg); i++) {
vpit_timer_arg[i].vpit = NULL;
assert(!vpit_timer_arg[i].active);
}
done: done:
VPIT_UNLOCK(); VPIT_UNLOCK();
if (vpit) { if (vpit)
free(vpit); free(vpit);
}
} }
INOUT_PORT(vpit_counter0, TIMER_CNTR0, IOPORT_F_INOUT, vpit_handler); INOUT_PORT(vpit_counter0, TIMER_CNTR0, IOPORT_F_INOUT, vpit_handler);