vfs: add dentry_ref, a smart pointer for dentrys

Makes managing references easier.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
vfs: convert file::f_dentry to dentry_ref

Fixes possible leak in sys_open() and vfs_file::close() error cases.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
vfs: make error class build in C code

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
vfs: add C++ namei()

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
vfs: covert sys_stat, sys_statfs to C++ namei

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
tests: Add test to check access to static __thread variable

This is a regression test to check that access to
static __thread variable does not generates page fault.

Signed-off-by: Zika L'Etrange <zika.letrange@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
sem: implement sem_timedwait

Exactly the same as sem_wait but with a timer. Luckily, our semaphore
API already have an option to do that. Just use it.

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>
mmu: fix RCU page table walking

hw_ptep holds a pointer to a pt_element in rcu_ptr object, but this
is not the pointer that is protected by RCU, hw_ptep object are always
live on a stack and never accessed from more than one thread. The one
that is protected by RCU and can be changed concurrently resides in page
table itself. Fix that by making a pointer to point to atomic type and
use correct memory order during various accesses.

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
mmu: make hw_ptep class to be a template parametrized on pte level

Utilize c++ type system to make it harder to pass wrong level ptes to
functions that expect certain pte level. Also, since now each hw_pte
level is different type, we can implement accessors differently for
each level. That allows us to use RCU accessors only for levels which
point to pages that can bee rcu freed, namely, level 1 and 2.

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
mmu: remove size parameter from page ops class and file map/unmap functions

Now, when page level is encoded into hw_ptep, size parameter is no
longer needed since size can be deduced from pte itself.

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
mmu: consolidate (small|huge)_page mapper operations

Now, when page level is encoded into hw_ptep, no need to have separate
handlers for small and large leaf pages. Most of them are nearly
identical anyways.

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
tests: Rename tst-fsx.c to misc-fsx.c

It's a manual test case, so rename it to misc-fsx.c and remove it from
the blacklist.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Merge branch 'timerwheel' of https://github.com/tgrabiec/osv

"The series introduces a data structure implementing an optimized algorithm
for managing timers which is optimized for not-expiring timers.

Both async timers (global) and core (per-CPU) timers are switched
to use the new implementation.

After applying this timer_task::reschedule is no longer at the top of CPU
profile, its footprint is reduced from 2-3% to below 1%. The overhead is
coming now from the across-CPU garbage collection routine:
async_worker::borrow_task().

It doesn't seem to have any significant impact on the tomcat benchmark."

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
build: fix tst-static-thread-variable.so dependencies

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
posix: Add getcontext, setcontext, and makecontext

These functions are POSIX functions for saving
and restoring thread context. The are used by programs
written in Go language.

Reviewed-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Zika L'Etrange <zika.letrange@gmail.com>
[ penberg: fix aarch64 build ]
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
pvclock: fix system_time() atomic-read logic

pvclock on the host ensures that when the update is complete the
version is even and when update is in progress the version is odd. It
is not enough to check if versions are equal because such sequence of
events is possible:

  Host                 Guest
  ====                 =====

              V = 0
              field1 = A1
              field2 = A2

  ++V == 1

  field1 = B1

                       V1 = V (== 1)
                       R1 = field1
                       R2 = field2
		       V2 = V (== 1 == V1)

  field2 = B2

  ++V == 2

                      R1 = B1
                      R2 = A2 (<-- wrong)

We should also check if update is in progress by checking if the
version is odd.

After fixing this, the following occasional assertion failure while
running 'make check' seems to go away:

Assertion failed: timestamp >= _last (/home/avi/osv-maint/include/osv/timer-set.hh: expire: 162)

[backtrace]
0x00000000003b54ae <sched::timer_list::fired()+1054>
0x000000000036de66 <interrupt_descriptor_table::invoke_interrupt(unsigned int)+166>
0x000000000036df59 <interrupt+41>
0x000000000036cb4c <interrupt_entry_common+34>
0x000000000038df3a <AcpiPsCreateOp+218>
0x000000000038d81d <AcpiPsParseLoop+349>
0x000000000038eb97 <AcpiPsParseAml+567>
0x00000000003762ff <AcpiDmParseDeferredOps+927>
0x0000000000376490 <AcpiDsGetRegionArguments+64>
0x0000000000388d3b <AcpiNsExecModuleCodeList+875>
0x000000000038ba50 <AcpiNsWalkNamespace+288>
0x000000000038c155 <AcpiWalkNamespace+165>
0x0000000000388ed1 <AcpiNsInitializeObjects+65>
0x00000000003989c4 <AcpiInitializeObjects+100>
0x000000000035ace5 <acpi::init()+21>
0x0000000000210346 <main_cont(int, char**)+326>
0x00000000003b7faa <thread_main_c+26>
0x000000000036cb75 <thread_main+7>
0x0000000000d19f4f <workman::_duty+2566>

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
x64: add lfence accessor

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
pvclock: add LFENCE before reading the time stamp counter

The RDTSC instruction is unordered, ensure that the processor orders it
with an LFENCE instruction.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
tests: add monotonic clock test case

Ensure the clock doesn't go backwards.

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

"Add dentry_ref, a cousin to fileref, an automatic reference counted smart
pointer for dentries, and start using it."

Reviewed-by: Pekka Enberg <penberg@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>