Add -Wno-format-security to build

Some distributions have enabled gcc -Wformat-security by default.
This causes the build to raise format warnings even though -Wformat=0
is used.

Setting no-format-security for build consistency between distros.

Signed-off-by: Or Cohen <orc@fewbytes.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Test unhandled exception

Commit 7fc023e8eba872260932e1c4264caf3b0b3b6932 fixed issue #64, and
made unhandled exceptions work correctly.

This patch adds a test to tst-except.cc to test this - that an unhandled
exception really does call the custom termination handler that the C++
standard allows to set (before the above commit, this test would hang).

Unfortunately, gcc's libsupc++ tries very hard to ensure that the
terminination handler really does abort - if the handler returns, or
throws an exception, the library nevertheless aborts. So we need to
trick it with an ugly longjmp to make this test be able to complete
without aborting the system.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
dhcp: remove superfluous comment

Signed-off-by: Benoit Canet <benoit@irqsave.net>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
dhcp: Configure the IP settings only after the dhcp server committed them.

The new behavior wait for the DHCP_ACK to be received by OSv to configure the
network parameters.

Signed-off-by: Benoit Canet <benoit@irqsave.net>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
dhcp: handle DHCP_MT_NAK packets by restarting the DHCP configuration

Follow section 3.1.5 of RFC2131.

Signed-off-by: Benoit Canet <benoit@irqsave.net>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
dlopen: return NULL in case of error

This patch fixes regression in d2397b7e434513e98fffe4f77b2886d0637ee3c0.
dlopen() must return NULL in case of error, pointer to shared_ptr holding NULL
returned instead.

Signed-off-by: Dmitry <dmitry@daynix.com>
run: auto-detect the hypervisor

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
build: run mkfs with one vcpu

smp doesn't currently work on non-accelerated qemu, and doesn't help anyway
during mkfs.  Restrict ourselves to 1 vcpu to avoid problems.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
percpu: Fix arithmetic on pointer to void

Spotted by Clang:

In file included from ../../loader.cc:27:
In file included from ../../drivers/virtio-net.hh:12:
In file included from ../../bsd/sys/net/if_var.h:80:
In file included from ../../bsd/sys/sys/mbuf.h:40:
In file included from ../../bsd/porting/uma_stub.h:161:
../../include/osv/percpu.hh:35:42: error: arithmetic on a pointer to void
        return reinterpret_cast<T*>(base + offset);
                                    ~~~~ ^

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
build: Add 'werror' option to Makefile

Add 'werror' option to Makefile that can be used to disable the -Werror
GCC option temporarily as follows:

  make werror=0

This is useful when fixing up build breakage caused by addition of new
default compiler warnings.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
bsd: Fix printf format string

Spotted by GCC with '-Wformat=1':

../../bsd/sys/netinet/tcp_subr.c: In function ‘tcp_log_addr’:
../../bsd/sys/netinet/tcp_subr.c:2286:3: warning: unknown conversion type character ‘b’ in format [-Wformat=]
   sprintf(sp, " tcpflags 0x%b", th->th_flags, PRINT_TH_FLAGS);
   ^
../../bsd/sys/netinet/tcp_subr.c:2286:3: warning: too many arguments for format [-Wformat-extra-args]

BSD has some nice printf extension for printing out bitfields. We don't
so switch to hexadecimal output.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
drivers/xenfront-xenbus: Fix scanf() variable type

Spotted by GCC with '-Wformat=1':

../../drivers/xenfront-xenbus.cc: In function ‘XenbusState xenbus_get_state(device_t)’:
../../drivers/xenfront-xenbus.cc:219:80: warning: format ‘%d’ expects argument of type ‘int*’, but argument 6 has type ‘XenbusState* {aka xenbus_state*}’ [-Wformat=]
     xs_scanf(XST_NIL, dev->get_node_path().c_str(), "state", NULL, "%d", &state);

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
x64: fix xapic INIT IPI

xapic::init_ipi() shifts apic_id by 24, unaware that xapic::ipi will do it
again.  The result is that the boot processor is reset instead of the
auxiliary processor.

Remove the extraneous shift.

Found by booting with QEMU without kvm.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
x64: fix APIC ID register read on QEMU

The expression *p >> 24, where p is an unsigned*, is optimized by
the compiler to *((u8*)p + 3) - reading the most significant byte only and
dropping the shift.

When this optimization is applied to reading the APIC ID, QEMU
returns zero for all processors, since the manual requires reading
entire words.

Fix by using a volatile pointer, disabling the optimization.

Note that QEMU is technically correct here though it violates all known
real x86 implementations.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
cpio: simplify stream creation

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
acpi: timer.set() requires an absolute value

The sched::timer set() function expects an absolute time value. Fix an
incorret use of it AcpiOsWaitSemaphore().

Signed-off-by: Benoit Canet <benoit@irqsave.net>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
build: omit source file from autodependencies

When converting a source file from .c to .cc, make will complain that the
old source file was missing.  This is annoying, especially when bisecting
or compile-testing a patch set.

Fix by removing the source file dependency.  Since the dependency is alread
specified explicitly by the makefile, no information is lost.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
vfs: fix ancestry check in rename()

Renaming to destination which is a substring of the source path
but not its parent should succeed:

  rename("/tmp/one_two", "/tmp/one") == 0

Renaming to a child should fail:

  rename("/tmp/", "/tmp/child") == -1 && errno = EINVAL

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
vfs: namei() should return ENOTDIR when component is not a directory

The call to namei("/dir/file/") currently fails with ENOENT when
"/dir/file" exists. A more standard way is to return ENOTDIR
instead. This way calls to stat, open, rename, etc. will be in
line with the POSIX spec.

It is also useful to rename() implementation which needs to
differentiate behaviour between the case in which target does not
exist and the case in which it does but the path has trailing slash
and the last component is not a directory.

In addition to that the check was performed in an inconsistent matter
- only when dentry lookup failed. This change makes the check
performed always.

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
vfs: fix incorrect rename() behavior with trailing slashes

Fix 1. Renaming existing directory to existing file when the
destination path has trailing slash should fail:

  rename("/tmp/dir", "/tmp/file/") == -1 && errno == ENOTDIR

Fix 2. Renaming exisitng directory to nonexisting path when the path
has trailing slash should succeed:

  rename("/tmp/dir", "/tmp/im_missing/") == 0

Fix 3. Renaming exisitng file to nonexisting path when the path
has trailing slash should fail:

  rename("/tmp/file", "/tmp/im_missing/") == -1 && errno == ENOTDIR

Reference:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html

This change also adds a bunch of test cases for various conditions
mentioned in the spec. Note that for some cases where the raname()
should fail there are discrepancies between Linux and OSv in error
codes set by the call. To my understanding, the spec is ambiguous.
There are conditions which match more than one error code
description. The tests are therefore allowing the call to set error
code from a set of matching error codes (eg. both EEXIST and ENOTEMPTY
are allowed when applicable). If we decide that OSv should return
exactly the same error codes as Linux for the same conditions, this
can be changed later.

vfs: rename() should fail if paths differ only with trailing slash

This should fail:

  rename("/tmp/file", "/tmp/file/")

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