x86: fix dump_registers

We are usign %x instead of %lx for x86_64. This has the effect of trimming all
64 bits to 32 bits creating a very, very confusing situation for people
debugging OSv.

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
trace: per-cpu trace buffers + event terminators

Per-cpu trace buffers. Actual buffer space is kept at roughly the "same"
as previously, up to 4 vcpu. Above this used space will be higher.
Does not handle vcpu:s appearing or disappearing in runtime.

Trace events are allocated with a "not done" terminator marker, which is
finalized when event is written, which should prevent any partial data
messing up extraction.

Fixes #146

Signed-off-by: Calle Wilund <calle@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
malloc: fix MALLOC_ALIGNMENT constant

The MALLOC_ALIGNMENT constant is supposed to be the default minimum
alignment returned by malloc(). I set it to 8, but this is actually not
what is normally done by recent modern of glibc on Linux, which takes
the maximum of 2*sizeof(size_t) (which is indeed 8) and the
alignof(long double), which is 16 bytes. So to be ABI compatible with
Linux, we should do the same.

Note that while this patch is good for future reference, it doesn't
actually have any practical consequences in the current implementation:
In our current small-malloc implementation, MALLOC_ALIGNMENT is not used
at all, and the alignment is determined just by the size of the allocated
object: A 16-byte object would have 16-byte alignment anyway. For large
allocations the alignment will always be a full page, so again this
change doesn't matter either.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
mmu: separate lock for vpopulate and friends

When the debug allocator is active, it will call vpopulate() to manage the
page table.  This causes a deadlock, as vpopulate() takes vma_list_mutex,
while other operations that hold vma_list_mutex may allocate memory.

Fix by using a separate mutex for the debug range.  This is safe since the
page table root is pre-allocated, and any lower page tables will be either
in the vma list range, or the debug range, but not both.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
virtio: fix virtio-blk under debug allocator

The debug allocator can allocate non-contiguous memory for large requests,
but since b7de9871b31 it uses only one sg entry for the entire buffer.

One possible fix is to allocate contiguous memory even under the debug
allocator, but in the future we may wish to allow discontiguous allocation
when not enough contiguous space is available.  So instead we implement
a virt_to_phys() variant that takes a range, and outputs the physical
segments that make it up, and use that to construct a minimal sg list
depending on the input.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
elf: Support on-load symbol resolution

Normally, symbol binding in shared objects is lazy, using the PLTGOT
mechanism. This means that a symbol is resolved only when first used.
This is great because it speeds up object load, and also allows us never
to implement symbols which aren't actually used in any real code path.

However, as issue #256 shows, symbols which are used in DSOs from a
preemption-disabled context cannot be resolved on first use, because
symbol resolution may sleep. Two important examples of this are
sched::thread::wait() and sched::thread::stop_wait(), both used by
wait_until() while it is in preempt_disable.

This patch adds the missing support for the standard DT_BIND_NOW tag.
This tag can be added added to an object with the "-z now" ld option.
When an object has this tag, all its symbols should be resolved on
load time, instead of lazily (when first used).

Bug #256 can be fixed by linking tst-mmap.so with "-z now" (this will
be a separate patch).

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
mmu: separate lock for vpopulate and friends

When the debug allocator is active, it will call vpopulate() to manage the
page table.  This causes a deadlock, as vpopulate() takes vma_list_mutex,
while other operations that hold vma_list_mutex may allocate memory.

Fix by using a separate mutex for the debug range.  This is safe since the
page table root is pre-allocated, and any lower page tables will be either
in the vma list range, or the debug range, but not both.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
virtio: fix virtio-blk under debug allocator

The debug allocator can allocate non-contiguous memory for large requests,
but since b7de9871b31 it uses only one sg entry for the entire buffer.

One possible fix is to allocate contiguous memory even under the debug
allocator, but in the future we may wish to allow discontiguous allocation
when not enough contiguous space is available.  So instead we implement
a virt_to_phys() variant that takes a range, and outputs the physical
segments that make it up, and use that to construct a minimal sg list
depending on the input.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
fadvise64: alias it from fadvise

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
runtime: provide _exit and _Exit aliases

Right now doing the same as exit(), since we don't support the atexit handlers
anyway

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
stub posix_fallocate

MongoDB expects that call and would like to guarantee allocation of blocks
in the file. It does have a fallback, so for the time being I am just providing
the symbol. I have opened Issue #265 to track this.

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
pthread: implement spinlocks

While we take pride in having no spinlocks in the system, if an application
wants to use them, who are we to deny them this god given right? Some
applications will implement spinlocks through a pthread interface, which is
what I implement here.

We did not have any standard trylock mechanism, so one is provided. Other than
that, the interface is pretty trivial except for the fact that it seems to
provide some protection against deadlocks. We will just ignore that for the
moment and assume a well behaved application.

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
libc: cheat about version number

Since most applications running under OSv will be compiled against glibc on
Linux, we need to provide symbols for gnu_get_libc_version and
gnu_get_libc_release. Unless we find a case where we have to, we shouldn't
lie, though. Answer OSv specific data here.

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
tcp: expose through getsockopt values of TCP_KEEPINTVL and TCP_KEEPIDLE

We are currently setting those values okay, but getting them takes us to the
default clause. They are all very simples cases of copying a variable, so it's
just a matter of doing that.

Both options mentioned are used by MongoDB. TCP_KEEPINIT is not, but well, it
can't hurt.

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
bsd/linux: convert tcp sockopt from Linux to BSD numbers

The comment states that option names are the same for BSD and Linux, but that
is not true at all for a lot of them. Checking the constants, I have came up
with the following conversion list. Before this patch, MongoDB complains that
getsockopt to query the interval time returns an error.

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
gdb: fix "osv pagetable walk" command

mmu::page_table_root symbol is no longer global.
It will take more than that to make it work on ARM.

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Merge tag 'v2p-debug-v1' of https://github.com/avikivity/osv

This patchset fixes bad interaction between the debug allocator and virtio.

After this patchset, 'make check' still fails, but this is due to genuine
use-after-free problems in the vfs, so will be fixed later.

Fixes #240.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
vfs: leak: Release refcnts hold by main_task structure on shutdown

Whenever you change the working directory through either fchdir or chdir,
a file structure holding refcnts to the underlying mount point is set to
the field t_cwdfp of main_task. If t_cwdfp was previously set, then the
older data will be freed and replaced by the newer one.
The problem exists because these refcnts are never released afterwards.

Changes introduced by this patch:
1) Create a function and use lambda to reuse as much code as possible
(Suggested by Glauber Costa).

2) Create vfs_exit that will use the function previously created to
free up main_task resources, and call unmount_rootfs afterwards.
Needless to say, vfs_exit replaces the function unmount_rootfs at the
shutdown procedure.

After applying the changes described above, the refcnt leaks on the root
mount point are gone.

Reviewed-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Raphael S. Carvalho <raphaelsc@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
trace: implement __lt__ for Trace

Python3 no longer supports __cmp__, we need to define __lt__ to make
the objects comparable. This fixes `trace extract` on GDB linked with
python3.

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

Enhance our runtime with the getgrnam function. Although we have no groups,
we will recognize group "nobody" for compatibility and return that as group
0

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>