backtrace: show call-sites instead of return-sites

I think this is more useful. When I analyze a backtrace I want to see
a call-chain rather than return-chain. The return addres may lay
inside an inlined function and have little to do with the
call-site. Example follows showing addresses translated by addr2line.

Before:

  tcp_net_channel_packet
  std::__atomic_base<unsigned int>::load(std::memory_order)
  operator()

After:

  tcp_net_channel_packet
  std::function<void (mbuf*)>::operator()(mbuf*)
  tcp_flush_net_channel

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
README.md: Fixed Java and C++ examples with -e option

New in v2:
 - Added new examples instead of just deletion of the invalid old ones.

Signed-off-by: Vlad Zolotarov <vladz@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
acpi: move some part of table initialization earlier

We would like to be able to use some ACPI-dependentant functionality earlier.
One example of that is hpet, that needs to consult some acpi tables and that is
run before acpi full initializatio. That particular case is already covered and
we provide the early_init function for that.

There is now another case: In order for poweroff() to work, we need the tables
to be actually loaded, as found out by Nadav. Those can also be moved inside
early_init.

After this patch, the functions:

    AcpiInitializeSubsystem(),
    AcpiReallocateRootTable(),
    and AcpiLoadTables().

are executed early. This is safe, because the part that really needs to come
after the APIC initialization is AcpiEnableSubsystem().

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
loader: power off when command line is invalid

Currently, in several cases when a bad command line is set in the image,
such as an empty command line (as in "make image=empty") or one with
invalid paramters (e.g., run.py -e "-a a"), we use abort(). abort() has two
annoying "features" - it hangs the VM forever, and shows an ugly stack
trace. Both are useful for a debugging - but it doesn't make sense to use
a debugger when just the command line is misconfigured - we just need to
print a message and power off the VM.

Calling osv::poweroff() in this early time during the boot is fine after
the previous patch which fixed osv::poweroff().

By the way, running a non-existant file (e.g., 'run.py -e a') already
had this correct behavior of powering off, not hanging.

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

When sblock() fails we left "so" locked, which would cause that the
current thread will hold the lock forever. Spotted during code
inspection.

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
net: extract common code

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
net: drain net channel queue before socket is closed

Fixes #259.

Commit 8340e21b58b12af72d886fdfd58d307340d19e47 introduced an
assertion which verifies that tcpcb and socket are not freed during
tcp_net_channel_flush(). This is justified because
tcp_net_channel_flush() is called from places which assume these will
remain live. Bug #259 revealed a situation in which this does not
hold. It is possible that after socket is closed and moved to
FIN_WAIT_1 state there are still some packets in its net
channel. Later on when these packets will be flushed, tcp_do_segment()
will try to reset the connection according to the protocol. This must
not happen during tcp_net_channel_flush() and we hit an asseriton
failure.

To solve the problem, we must ensure that tcp_do_segment() will not
try to close TCP when it's called on packets from a net channel. Let's
walk over all possible places (which I was able to find):

 * Inside if (thflags & TH_RST) ...

   This should not happen because net channel classifier does not let
   in packets with RST, FIN or SYN flags.

 * Inside if ((so->so_state & SS_NOFDREF) &&
         tp->t_state > TCPS_CLOSE_WAIT && tlen)

   This is the one we hit in issue #259. In order to avoid it we must
   ensure that all packets from the net channel are drained after the
   classifier is unregistered but before the socket is marked as
   SS_NOFDREF (closed). This is fixed by this patch.

 * t_state == TCPS_LAST_ACK && ourfinisacked

   state is moved to TCPS_LAST_ACK when socket is closed while in
   TCPS_CLOSE_WAIT state. That state move happens after net channel
   classifier is unregstered in reaction to received FIN. After that
   we send our own FIN. Because the other side will send the final ACK
   after that, that final ACK cannot land in the net channel, so we
   should be good.

 * Entering TIME_WAIT state via tcp_twstart()

   tcp_twstart() frees the socket. This is not catched by "want_close"
   flag, but is as much prohibited. To enter TIME_WAIT we must first
   enter FIN_WAIT_1 and then receive ACK from the other side for our
   FIN (among other things). Because the classifier is unregistered
   before entering FIN_WAIT_1, the final ACK cannot go through the net
   channel. Therefore, tcp_net_channel_flush() cannot trigger
   TIME_WAIT state and we should be good.

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Merge branch '259'

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
net: don't reset connections that overflow the listen backlog queue

The networking stack has a queue of connections that were established but
not yet accepted.  Connections that exceed the length of the queue are dropped.

The stack has two ways of dropping a connection: sending an RST or silently.
The default, sending an RST, will affect the sender, while dropping the
packet silently will allow the sender to retry and hopefully allow the
connection to be established.

Change the default to silently dropping the packet, as it is more friendly
to users, and matches Linux.

Seen with iperf -P 16, which quickly overwhelms the listen backlog and causes
a few streams to fail.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
elf: add functions to read the section table and section table entry names

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
elf: populate segments if ".note.osv-mlock" section is present

This allows shared objects that disable preemption to work correctly,
since faulting in executable pages with preemption disabled is not supported.

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
elf: fix bad indents

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
rcu: destroy callback after executing it

The callback's destructor may refer to dynamically linked sections; destroy
it after executing it to prevent the destructor firing in the next pass and
referencing deallocated memory.

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
rcu: add rcu_flush()

Add a callback to force all currently queued callbacks to execute.

This is useful for global state changes, such as removing a loaded module,
and for unit tests.

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
elf: flush all rcu callbacks before unloading a module

An rcu callback may refer to the text or data section of a loaded module;
prevent access to unmapped memory by forcing all rcu callbacks to complete
before removing the module.

Fixes #231.

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
rcu: introduce an rcu list type

This is a singly-linked list, suitable for building an rcu hash table.

Only a minimal interface is implemented so far.  The list itself exposes
two list-like interfaces, one for a mutating owner, the other for read-only
rcu access.

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
memory: support for larger-than-page alignment

Our existing implementation of posix_memalign() and the C11 aligned_alloc()
used our regular malloc(), so it only work worked up to alignment of 4096
bytes - and crashed when it failed to achieve a higher desired alignment.

Some applications do ask for higher alignment - for example MongoDB allocates
a large buffer at 8192 byte alignment, and accordingly crashes half of the
times, when the desired alignment is not achieved (half of the time, it is
achieved by chance).

This patch makes our support for alignment better organized, and fixes
the alignment > 4096 case:

The alignment is no longer available only to the outer function like
posix_memalign(). Rather, it is passed down to lower-level allocation
functions like malloc_large() which allocates whole pages - and this
function now knows how to pick pages which start at a properly aligned
boundary.

This patch does not improve the wastefulness of our malloc_large(), so
an overhaul of it would still be welcome. Case in point, malloc_large()
always adds a full page to any allocation larger than half a page.
Multiple allocations with posix_memalign(8192, 8192), rather than being
tightly packed, each take 3 pages and are separated by a free page.
This page is not wasted, but causes fragmentation of the heap.

Note that after this patch, we still have one other bug in
posix_memalign(size, align) - for small sizes and large alignments.
For small sizes, we use a pool allocator with "size" alignment, and
may not achieve the desired alignment (so causing an assertion failure).
This bug can also be fixed, but is unrelated to this patch.

This patch also adds a test for posix_memalign(), checking all alignments
including large alignments which are the topic of this patch.
The tests for small *sizes*, which as explained above are still buggy,
are commented out, because they fail.

Fixes #266.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@avi.cloudius>
tests: add rcu list test

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Merge branch 'rcu-list'

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
zfsarc: temporarily disable ARC mappings

The fix will take time. Just disable it entirely while we cook up a solution.

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