/*-
 * Copyright (c) 2008 Robert N. M. Watson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/mman.h>
#include <sys/time.h>

#include <err.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "/usr/src/sys/dev/evilmem/evilmem.h"

int
main(int argc, char *argv[])
{
	struct timespec em_ts, syscall_ts;
	int em_fd, em_generation_before, em_generation_after;
	struct evilmem *em;

	em_fd = open("/dev/evilmem", O_RDONLY);
	if (em_fd < 0)
		err(-1, "open: evilmem");

	/* XXXRW: Should have an ioctl to request page size for evilmem. */
	em = mmap(NULL, getpagesize(), PROT_READ, MAP_SHARED, em_fd, 0);
	if (em == MAP_FAILED)
		err(-1, "mmap");
	close(em_fd);

	if (em->em_magic != EVILMEM_MAGIC)
		errx(-1, "evilmem magic");
	do {
		em_generation_before = em->em_generation_before;
		em_ts = em->em_time;
		em_generation_after = em->em_generation_after;
	} while (em_generation_before != em_generation_after);

	if (clock_gettime(CLOCK_REALTIME, &syscall_ts) < 0)
		err(-1, "clock_gettime");

	printf("evilmem: %jd.%08jd\n", (intmax_t)em_ts.tv_sec,
	    (intmax_t)em_ts.tv_nsec);
	printf("syscall: %jd.%08jd\n", (intmax_t)syscall_ts.tv_sec,
	    (intmax_t)syscall_ts.tv_nsec);
	return (0);
}
