Fix HPET driver clock rollback

When KVM paravirtual clock isn't available (e.g., on Xen or on plain Qemu),
we used the HPET clock. Our HPET clock driver rolled back the clock
(clock::get()->time()) once every 42 seconds, causing strange things like
a scheduler assertion when the clock jumps back.

The problem is that we read just 32 bits out of the 64 bits of the HPET
counter. This means that we roll back the clock once every 2^32 ticks,
and with the 10ns tick (which seems to be case in Qemu), this means about
42 seconds.

Douglas Adams would have liked this bug ;-)

Fixed the code, and removed overly-optimistic comment which stated the
rollback should take years.

Added an assertion that the HPET really has a 64-bit counter; Intel's HPET
specification from 2004 already specify that a 64-bit counter is recommended,
and both Qemu and Xen do implement a 64-bit counter. If we had to deal
with a 32-bit counter, we would need to write a handler for the interrupt
that the HPET sends every time the counter wraps around.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
module.py: fix "make module" invocation

Shell command line should be passed as one argument. Additional
arguments are passed to the shell itself.

Reported-by: Nadav

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Fix exception (in x86 sense, not C++) handling during boot

During boot, between the time main() set the IDT and when later
smp_launch() is called, the IDT doesn't actually work correctly.
The problem is that we use the separate stacks feature (IST), and that
doesn't work without also setting the GDT, not only the IDT.

So use init_on_cpu() to initialize not only the IDT, but other stuff
as well. Fix smp_launch() not to repeat this initialization on the boot
CPU, as it was already done.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
zfs: remove fake struct file definition

We have an strange and incorrect 'struct file' definition for zfs, but
a forward declaration is all that is needed.

Remove the unsafe definition.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
vfs_syscalls.c: convert to C++

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Add helper for converting designated initializers to C++

Unfortunately, C++ does not support designated initializers.  Add a function
that helps fill their place.

Use example:

-static struct netisr_handler ether_nh = {
-       .nh_name = "ether",
-       .nh_handler = ether_nh_input,
-       .nh_proto = NETISR_ETHER,
-       .nh_policy = NETISR_POLICY_SOURCE,
-       .nh_dispatch = NETISR_DISPATCH_DIRECT,
-};
+static netisr_handler ether_nh = initialize_with([] (netisr_handler& x) {
+       x.nh_name = "ether";
+       x.nh_handler = ether_nh_input;
+       x.nh_proto = NETISR_ETHER;
+       x.nh_policy = NETISR_POLICY_SOURCE;
+       x.nh_dispatch = NETISR_DISPATCH_DIRECT;
+});

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
vfs_fops.c: convert to C++

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
ramfs_vnops: convert to C++

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
devfs_vnops: convert to C++

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
file: move file operations out-of-line

In preparation for making 'file' a C++ type.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
socketpair: fix wierd initialization

socketpair() stores temporary data in the field they're going to be copied
into.

Use a local instead.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
file: add helpers for accessing fields

Needed for C++ conversion.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
file: make it a C++ type

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Fix crash on malformed command line

Before this patch, OSv crashes or continuously reboots when given unknown
command line paramters, e.g.,

        scripts/run.py -c1 -e "--help --z a"

With this patch, it says, as expected that the "--z" option is not
recognized, and displays the list of known options:

    unrecognised option '--z'
    OSv options:
      --help                show help text
      --trace arg           tracepoints to enable
      --trace-backtrace     log backtraces in the tracepoint log
      --leak                start leak detector after boot
      --nomount             don't mount the file system
      --noshutdown          continue running after main() returns
      --env arg             set Unix-like environment variable (putenv())
      --cwd arg             set current working directory
    Aborted

The problem was that to parse the command line options, we used Boost,
which throws an exception when an unrecognized option is seen. We need
to catch this exception, and show a message accordingly.

But before this patch, C++ exceptions did not work correctly during this
stage of the boot process, because exceptions use elf::program(), and we
only set it up later. So this patch moves the setup of the elf::program()
object earlier in the boot, to the beginning of main_cont().

Now we'll be able to use C++ exceptions throughout main_cont(), not just
in command line parsing.

This patch also removes the unused "filesystem" paramter of
elf::program(), rather than move the initializion of this empty object
as well.

Fixes #103.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Add license/fastlog2.txt

Add license file (BSD license) for fastlog2() function used in the
new scheduler as a quick approximate logf().
From https://code.google.com/p/fastapprox/.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
devfs: device_destroy() API

Enable device_destroy() API for the virtio-rng driver.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
virtio: Fix device init with no MSI support

Not all virtio devices support MSI.  Fix device initialization by not
writing to VIRTIO_MSI_QUEUE_VECTOR register if a PCI device does not
advertise MSI-X support.  This is needed to initialize virtio-rng
devices when running on KVM/QEMU.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
virtio: Add virtio-rng driver

This adds the virtio-rng driver to OSv.  The implementation is simple:

  - Start a thread that keeps 64 byte of entropy cached in internal
    buffer.  Entropy is gathered from the host with virtio-rng.

  - Create device nodes for "/dev/random" and "/dev/urandom" that both
    use the same virtio_rng_read() hook.

  - Use the entropy buffer for virtio_rng_read().  If we exhaust the
    buffer, wake up the thread and wait for more entropy to appear.

We eventually should move device node creation to separate
drivers/random.c that multiplexes between different hardware RNG
implementations.  However, as we only support virtio-rng, I'm leaving
that to whomever implements support for the next RNG.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
tests: tst-random.so

Add a simple test case for "/dev/random" which just reads from the file
and prints out the random bytes.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Add java.security properties to the image

Without it secure random generator doesn't use /dev/random
source, which leads to slow startup of tomcat.

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>