build.mk: introduce *_env variables to make system

According to http://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html,
the versions of standard libraries have a strong matchup with versions
of compilers, so linkage errors occur when using libstdc++ in *external*
submodule when using GCC 4.9.0.

As there is such a mixup in the build system, the environments of
standard libraries in link time should be switchable to support GCC
whose version doesn't match up with external's.

*_env variables are introduced. To build a image with standard libraries
in the host, run `make build_env=host'. For fine-grained settings, use
gcc_lib_env and cxx_lib_env.

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Boqun Feng <boqun.feng@linux.vnet.ibm.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
tests: initial malloc benchmark

Currently covering three cases: 1 thread allocation, n threads allocation,
and n threads allocations with frees in a different cpu. We should improve
with more cases.

This test was designed to run on Linux as well. Let's keep it this way.

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
vfs: enable support for DEBUG_VFS

At the moment, defining DEBUG_VFS fails OSv compilation. The patch
ensures OSv compiles and correct debug logs are emitted.

Reviewed-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Prasad Joshi <prasadjoshi.linux@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
mmu, x64: get maximum supported phys/virt bits from cpuid

Later patches will use reserved bit, so we want to be sure that it is
available.

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
mmu, x64: fix getting/setting address in a pte

Currently address we get from pte includes reserved bits and setting
zeroes available and reserved bits.

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
mmu, x64: optimize detection of PROT_NONE mem region using PT reserved bit

Java uses accesses to PROT_NONE region to stop threads sometimes, so it
worthwhile to be able to catch this as fast as possible without taking
vma_list_mutex. The patch does it by setting reserved bit on all ptes in
PROT_NONE VMA which causes RSVD bit to be set in a page fault error code.
It is enough to check it to know that access is to a valid VMA, but
permission is lacking.

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
mmu: rename intermediate_page() to intermediate_page_post()

The function should be called intermediate_page_post(), otherwise it is
never called.

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
mmu: remove reference to noexistent hw_pte_ref class

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
mmu,x64: optimize detection of PROT_READ mem region by checking cow bit in a pte

Java uses PROT_READ page to synchronize threads, so it worthwhile to be
able to catch this as fast as possible without taking vma_list_mutex.
The patch does it by checking that pte is not marked as cow during write
fault on present pte since cow or PROT_READ are the only reasons for pte
to be write protected. The problem is that to get pte we need to walk
the page table, but access to a page table is currently protected by
vma_list_mutex. The patch uses RCU to free intermediate page table levels
which makes it possible to get to pte without taking vma_list_mutex lock.

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
tls: move more details about initialization to arch

the thread_control_block structure needs to be different
between x64 and AArch64;
For AArch64's implementation for local execution, try to
match the layout in glibc and the generated code.

Do not align .tdata and .tbss sections with .tdata : ALIGN(64)
or it will affect the TLS loads.

Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>
Cc: Glauber Costa <glommer@cloudius-systems.com>
Cc: Will Newton <will.newton@linaro.org>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
fix "osv info threads" gdb command

If thread is not started its cpu pointer will be NULL. Handle this
case properly.

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Reindent __newlocale()

The __newlocale() function in runtime.cc used a mixture of spaces and
tabs. Reindent it, with no changes to actual content (that would come
later).

Reviewed-by: Raphael S. Carvalho <raphaelsc@cloudius-systems.com>
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Don't abort() on unknown locale

When an application attempts to use an unknown locale, don't abort but
rather fail normally, as Linux does on an unknown locale.

Theoretically, Posix specifies newlocale() can also fail with EINVAL if
the category_mask is malformed. However, the only reasonable usage of this
function we support is when base=NULL (or "C" locale) and locale is again
"C", and then we just ignore the category_mask. If that is not the case,
we can just complain with ENOENT (meaning we couldn't find the named
locale).

In any case, callers like std::locale() don't actually care why newlocale()
failed, and anyway assume a failure means the localename wasn't recognized.

Reviewed-by: Raphael S. Carvalho <raphaelsc@cloudius-systems.com>
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
locale: fix shifted ctype array

Fixes #314.

In two's complement, the lowest signed 8-bit number is -128, not -127.
By wrongly starting to generate the ctype array starting with -127 instead
of -128, we got all the locale ctype questions shifted by one character;
For example character 32 (' ') was not considered a space, but 31 was!

We didn't see this bug because our C library isspace() and friends are
currently implemented without using the locale framework (which is fine,
as we only support the "C" locale anyway). However, this bug is
apparent in C++, as explained in issue #314: std::isspace() returns the
wrong answer, and C++ facilities which use this under the hood - such
as reading from an istream which is supposed to stop at a space - also
got broken.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
locale: add test for C++ locale support

This test catches a few bugs we had in our C++ locale support (see
issue #314). After the previous patches, this test will succeed.

This test verifies that:

1. Trying to use an unsupported locale (we only support "C") throws an
   exception, as required, rather than crashing as before.

2. std::isspace(' ',std::locale) returns true, as it should (previously
   our ctype array was shifted by one, so this returned false!)

3. istream's input operator (operator>>) should stop on a space.
   Previously, it didn't, because we didn't recognize the space.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
vfs: LFS64 versions of two more functions

preadv, pwritev.

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>
aarch64: entry.S: show exception vector entries in backtraces

by marking as @function we get them to show up in the backtrace.

Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
aarch64: arch-switch: implement thread::switch_to

Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
aarch64: mmu: fix page_fault assert for daif_i

In AArch64 the I-bit set in DAIF means "interrupts disabled".
Therefore, the assertion check must be inverted in page_fault.

Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
trace: use seconds as the default unit of timestamp

trace.py allows to slice samples to a time range using --since,
--until and --period. Currently the default unit for timestamps was
nanoseconds. However the timestamp pretty much always comes from the
'trace list' output, which displays timestamp in seconds with a dot
separating the nanosecond precision part. That required explicit unit
designating by appending 's' suffix to the timestamp when passing to
--since and --until to

This change makes --since and --until use seconds as a default unit,
so that the timestamp copied from trace list can be used without any
change, eg:

 $ scripts/trace.py --since 1400607382.775557041

Default unit of --period (nanoseconds) remains unchanged.

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