Commit graph

1698 commits

Author SHA1 Message Date
Watson Zeng
79fb3b7ccc libc: minimal: mwdt: define __INT*_C() and __UINT*_C()
Our minimal C library makes an alias of UINT*_C() to
be __UINT*_C() and INT*_C() to __INT*_C(). However,
in mwdt, these are not defined by default, so define
them ourselves. We have similar fix for xcc: #31962

Signed-off-by: Watson Zeng <zhiwei@synopsys.com>
2021-05-25 12:55:48 -05:00
Andy Ross
d37370301c k_heap: Clamp to a minimum heap size
The K_HEAP_DEFINE macro would allow users to specify heaps that are
too small, leading to potential corruption events (though at least
there were __ASSERTs that would catch this at runtime if enabled).

It would be nice to put the logic to compute this value into the heap
code, but that isn't available in kernel.h (and we don't want to pull
it in as this header is already WAY to thick).  So instead we just
hand-compute and document the choice.  We can address bitrot problems
with a test.

(Tweaks to heap size asserts and correct size bounds from Nicolas Pitre)

Fixes #33009

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2021-05-20 17:52:21 -04:00
Anas Nashif
1e74ddd709 kernel: remove dead workq code
work_q.c is not being built or used, it was replaced by user_work.c
which now has k_work_user_queue_start.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-05-18 11:21:27 -05:00
Carles Cufi
14b358a252 libc: minimal: Add EOVERFLOW
Add the EOVERFLOW error number, which is already in use by some of
Zephyr's modules.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2021-05-17 09:11:15 -04:00
Carles Cufi
1e26e08a92 libc: minimal: Add doxygen formatting to errno.h
Format the file so it can be processed by Doxygen.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2021-05-17 09:11:15 -04:00
Carles Cufi
165def7ea6 libc: minimal: Align errno.h values with newlib
In order to be able to document the error codes with Doxygen and
Breathe, start by aligning the macro values to those provided by newlib
in include/sys/errno.h:

https://github.com/zephyrproject-rtos/newlib-cygwin/blob/zephyr-newlib-4.1.0/newlib/libc/include/sys/errno.h

Also get rid of the (unused and untrue) ERRMAX definition.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2021-05-17 09:11:15 -04:00
Stephanos Ioannidis
1ff6664e98 lib: newlib: Remove internal _sbrk lock
This commit removes the lock inside the newlib internal `_sbrk`
function, which is called by `malloc` when additional heap memory is
needed.

This lock is no longer required because any calls to the `malloc`
function are synchronised by the `__malloc_lock` and `__malloc_unlock`
functions.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2021-05-13 07:30:43 -05:00
Stephanos Ioannidis
b0e690ac1e lib: newlib: Add malloc lock
This commit adds a lock implementation for the newlib heap memory
management functions (`malloc` and `free`).

The `__malloc_lock` and `__malloc_unlock` functions are called by the
newlib `malloc` and `free` functions to synchronise access to the heap
region.

Without this lock, making use of the `malloc` and `free` functions from
multiple threads will result in the corruption of the heap region.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2021-05-13 07:30:43 -05:00
Maksim Masalski
dbfa5a04de coding guidelines 11.9: fix literal zero as the null-ptr-constant
File has next violations:
MISRA 11_9_a
Use NULL instead of literal zero (0) as the null-pointer-constant
MISRA 11_9_b
Literal zero (0) shall not be used as the null-pointer-constant

Signed-off-by: Maksim Masalski <maksim.masalski@intel.com>
2021-05-12 21:51:40 -04:00
Maksim Masalski
136c9424c5 coding guidelines 10.4: casting operands to have same types
File zephyr/lib/os/cbprintf_nano.c had operands with different types.
It caused Rule 10.4 violation.
Both operands of an operator in which the usual arithmetic conversions
are performed shall have the same essential type category.

Signed-off-by: Maksim Masalski <maksim.masalski@intel.com>

coding guidelines 10.4: casting operands to have same types

File zephyr/lib/os/cbprintf_nano.c had operands with different types.
It caused Rule 10.4 violation.
Both operands of an operator in which the usual arithmetic conversions
are performed shall have the same essential type category.

Signed-off-by: Maksim Masalski <maksim.masalski@intel.com>

removed cast to int
2021-05-08 12:32:15 +02:00
Evgeniy Paltsev
6afe7c5fd2 ARC: prepare for building for ARCv3 HS6x
Do basic preparations for building code for ARCv3 HS6x
* add ISA_ARCV3 and CPU_HS6X config options
* add off_t type support for __ARC64__
* use elf64-littlearc format for linking
* use arc64 mcpu for CPU_HS6X

Signed-off-by: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
Signed-off-by: Evgeniy Paltsev <PaltsevEvgeniy@gmail.com>
2021-05-07 14:55:49 -05:00
Daniel Leung
ff407fb922 sys: introduce bit arrays
This introduces bit arrays as a new data type. This is different
than sys_bitfield as it is working on raw arrays of 32-bit
data. The bit arrays encode additional data inside the struct
to avoid going beyond the declared number of bits, and also
provides locking.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2021-05-07 13:36:22 -04:00
Krzysztof Chruscinski
edc3c87c72 lib: os: mpsc_buf: Allow dropping of skip packets in no overwrite
Previously, when no overwrite mode was used and there was no space
no packet was dropped. However, it should be allowed to drop skip
packet that may be added as padding at the end of the buffer.
Extended dropping scheme to drop skip packets in no overwrite mode.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2021-05-06 15:40:05 +02:00
Krzysztof Chruscinski
7825487334 lib: os: mpsc_pbuf: Add guard for oversized allocation
Added early return from mpsc_pbuf_alloc when requested size
exceed the buffer capacity. Previously, in that case buffer
was falling into endless loop.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2021-05-06 15:40:05 +02:00
Guennadi Liakhovetski
91d90df2a7 p4wq: add support for per-CPU, per-queue and delayed threads
Currently P4WQ supports queues with sets of user-provided
worked threads of arbitrary numbers. These threads are started
immediately upon initialisation.

This patch adds support for 3 more thread implementation options:

1. queue per thread. It adds a K_P4WQ_ARRAY_DEFINE() macro which
   initialises an array of queues and threads of the same number.
   These threads are then uniquely assigned to respective queues.
2. delayed start. With this option threads aren't started
   immediately upon queue initialisation. Instead a new function
   k_p4wq_enable_static_thread() has to be called to enable those
   threads individually.
3. queue per CPU. With this option the user can assign CPU masks
   to threads when calling k_p4wq_enable_static_thread().
   Otherwise the cpu_mask parameter to that function is ignored.
   Currently enabling this option implies option 2 above. Also so
   far to enable queues per CPU the user has to use
   K_P4WQ_ARRAY_DEFINE(), which means this option also implies 1
   above, but both these restrictions can be relaxed in the
   future if required.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2021-05-03 17:13:01 -04:00
Guennadi Liakhovetski
56610bdafb p4wq: fix races when handling work items
Work items in P4WQ currently belong to the user before submission
and after exit from the handler, therefore, unless the handler
re-submits the item, accessing it in p4wq_loop() in such cases
is racy. To fix this we re-define work item ownership. Now the
item belongs to the P4WQ core until the user calls
k_p4wq_wait(). If the work item has its .sync flag set, the
function will sleep until the handler completes processing the
work item or until the timeout expires. If .sync isn't set and
the handler hasn't processed the item yet, the function returns
-EBUSY.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2021-05-03 17:13:01 -04:00
Guennadi Liakhovetski
a4b88193cc p4wq: fix initialisation with SMP disabled
When SMP is disabled, the SMP initialisation level is
undefined, therefore a different level must be used.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2021-05-03 17:13:01 -04:00
Daniel Leung
143a18b36b lib: os: assign sys_notify callback default to NULL...
...instead of numeric zero.

Current usage violates MISRA rule 11.9.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2021-04-29 07:16:37 -04:00
Jennifer Williams
b773ec1675 lib: os: cbprintf_complete: fix typo in cbvprintf width comment
This commit fixes a subtle typo - width instead of with.

Signed-off-by: Jennifer Williams <jennifer.m.williams@intel.com>
2021-04-29 07:14:39 -04:00
Gerard Marull-Paretas
a7e4ae5976 lib: os: reboot: misc cleanup
Remove redundant includes and out-of-date comment.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-04-28 20:34:00 -04:00
Gerard Marull-Paretas
f163bdb280 power: move reboot functionality to os lib
Reboot functionality has nothing to do with PM, so move it out to the
subsys/os folder.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-04-28 20:34:00 -04:00
Jennifer Williams
be5a3777ca lib: os: add final else where missing in onoff, p4wq, sem
onoff, p4wq, and sem had several places missing final else
statement in the if else if construct. This commit adds
else {} to comply with coding guideline 15.7.

Signed-off-by: Jennifer Williams <jennifer.m.williams@intel.com>
2021-04-28 20:28:19 -04:00
Jennifer Williams
9517b87d35 lib: os: add final else where missing in heap*
heap* had several places missing final else statement in the
if else if construct. This commit adds else {} to comply with
coding guideline 15.7.

Signed-off-by: Jennifer Williams <jennifer.m.williams@intel.com>
2021-04-28 20:28:19 -04:00
Jennifer Williams
254dfd4aba lib: os: add final else where missing in cbprintf_*
cbprintf_* had several places missing final elsestatement in the
if else if construct. This commit adds else {} to comply with
coding guideline 15.7.

Signed-off-by: Jennifer Williams <jennifer.m.williams@intel.com>
2021-04-28 20:28:19 -04:00
Daniel Leung
2c6da987cd lib/os: cbprintf_packaged: simplify rodata check
NIOS2 is using _image_rodata_start/_end in its linker script
to mark the boundaries of rodata. So they no loner need
special treatment.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2021-04-28 22:25:42 +02:00
Anas Nashif
b8312fab4c Revert "lib: os: various places fix missing final else"
This reverts commit 163b7f0d82.

This is causing test failures, see #34624

Fixes #34624

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-04-27 22:42:00 -04:00
Jennifer Williams
163b7f0d82 lib: os: various places fix missing final else
The lib/os/ had several places missing final else
statement in the if else if construct. This commit adds
else {} or simple refactor to comply with coding guideline 15.7.
- cbprintf_complete.c
- cbprintf_nano.c
- heap-validate.c
- heap.c
- onoff.c
- p4wq.c
- sem.c

Also resolves the checkpatch issue of comments should align * on
each line.

Signed-off-by: Jennifer Williams <jennifer.m.williams@intel.com>
2021-04-27 17:31:59 -04:00
Krzysztof Chruscinski
45baec2d5b lib: gui: lvgl: Fix logging macro usage
Logging macro is expecting a constant value.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2021-04-22 15:29:15 -05:00
Flavio Ceolin
3e38d5efad lib: rb: Fix violations to rule 12.1
Missing explicit parentheses on sub-expressions

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-04-20 15:50:49 -04:00
Flavio Ceolin
f83c004319 lib: rb: Fix violations to rule 14.4
get_child does not return an essentially boolean type, so it has to be
properly checked against a pointer.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-04-20 15:50:49 -04:00
Flavio Ceolin
81b5b030d5 lib: rb: Fix violation to rule 10.3
get_color return enum rb_color. Use this type to avoid
implicit conversion to int.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-04-20 15:50:49 -04:00
Flavio Ceolin
188bb6e160 lib: rb: Use types that indicate size and signedness
Fixes violations related with essential type model.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-04-20 15:50:49 -04:00
Anas Nashif
a15f32f58c portability: move cmsis APIs to subsys/portability
Move cmsis OS apis under subsystem/portability. Those are not libraries
and only serve to provide a level of abstraction using the CMSIS OS APIs
to existing Zephyr interfaces.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-04-20 08:45:05 -04:00
Krzysztof Chruscinski
f1bb20f6b4 logging: Adapt logger to support both versions
Adapted to be able to switch between v1 and v2.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2021-04-19 10:59:23 -04:00
Krzysztof Chruscinski
345d12e8e0 tests: lib: Add test for mpsc_pbuf
Added suite for multiple producer, single consumer packet buffer.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2021-04-19 10:59:23 -04:00
Krzysztof Chruscinski
1fd71ca043 lib: os: mpsc_pbuf: Add optional debug features
Added optional debug prints. Logging cannot be used because
mpsc pbuf is used by the logging.

Added option to clear packet memory after allocation. Option is
enabled in Kconfig.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2021-04-19 10:59:23 -04:00
Krzysztof Chruscinski
3a765f4384 lib: os: Add mpsc (multiple producer, single consumer) packet buffer
Added module for storing variable length packets in a ring buffer.
Implementation assumes multiple producing contexts and single consumer.

API provides zero copy functionality with alloc, commit, claim, free
scheme.

Additionally, there are functions optimized for storing single word
packets and packets consisting of a word and a pointer. Buffer can work
in two modes: saturation or overwriting the oldest packets when buffer
has no space to allocate for a new buffer.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2021-04-19 10:59:23 -04:00
Jukka Rissanen
dde03c6770 net: socket: Add locking to prevent concurrent access
The BSD API calls were not thread safe. Add locking to fix this.

Fixes #27032

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2021-04-15 07:16:51 -05:00
Jennifer Williams
efc78b5b46 lib: os: fix heap_print_info missing final else in construct
The if ... else if ... construct was missing the final else.
This commit refactors it to comply with coding guideline 15.7.
The logic is to check if used or free, and do not increment
for the reserved chunks (first/last) in the heap.

Signed-off-by: Jennifer Williams <jennifer.m.williams@intel.com>
2021-04-14 09:20:20 -04:00
Jordan Yates
a4afedb02a sys: ring_buffer: allow NULL data buffers
Allow NULL data buffers to be provided to `ring_buf_get` and
`ring_buf_item_get`, in which case data will be discarded instead of
copied out to the user.

Fixes #33488.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-04-09 13:19:30 +02:00
Anas Nashif
0ec3774bde userspace: rename _is_user_context -> k_is_user_context
This functions is being called across the tree, no reason why it should
not be a public API.

The current usage violates a few MISRA rules.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-04-01 05:34:17 -04:00
David Leach
dca4397e08 subsys/cmsis_v1: Fix unitialized struct coverity error
Fixes #33060

Signed-off-by: David Leach <david.leach@nxp.com>
2021-04-01 04:52:10 -04:00
Carlo Caione
3539c2fbb3 arm/arm64: Make ARM64 a standalone architecture
Split ARM and ARM64 architectures.

Details:

- CONFIG_ARM64 is decoupled from CONFIG_ARM (not a subset anymore)
- Arch and include AArch64 files are in a dedicated directory
  (arch/arm64 and include/arch/arm64)
- AArch64 boards and SoC are moved to soc/arm64 and boards/arm64
- AArch64-specific DTS files are moved to dts/arm64
- The A72 support for the bcm_vk/viper board is moved in the
  boards/bcm_vk/viper directory

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2021-03-31 10:34:33 -05:00
Nicolas Pitre
7b6a4da206 libc/minimal: assorted fixes to malloc() and friends
- When malloc() is called with a size of 0 we should not set errno
  to ENOMEM as there is no actual allocation failure in that case.
  This duplicates the realloc() behavior.

- Put unlock_ret assignments on separate lines, otherwise gcc complains
  about unused variables when the tests on it are disabled.

- There NULL return added in 952970d6cb are completely pointless.
  First, there is no reason for sys_mutex_unlock() to fail, and even
  if it did, those returns would be blatent memory leaks. Remove them.
  No one should blindly modify code just to make static code
  analysers happy.

- Replace all CHECKIF() by explicit assertion statements to uniformize
  those checks and drop the NULL returns entirely. We can't return
  anything in the free() case, and there are no runtime conditions
  for sys_mutex_lock() to sometimes succeed and sometimes fail anyway.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-03-31 08:05:37 -04:00
Artur Lipowski
3b269bb033 lib: cmsis_rtos_v2: Fix timing and logic for osEventFlagsWait.
Consistently use ticks for timing purposes.
Fix retry logic.
Check flags when osFlagsWaitAll is not set.

Fixes #31103

Signed-off-by: Artur Lipowski <Artur.Lipowski@hidglobal.com>
2021-03-29 13:52:12 -04:00
Krzysztof Chruscinski
65474d8d67 lib: os: cbprintf: Improvements for sparc
Unified define used for handling sparc case in static and
runtime packaging. Reworked macro for storing argument in
static packaging.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2021-03-29 14:08:41 +02:00
Krzysztof Chruscinski
c26e08a51f lib: os: cbprintf: Add alignment offset to packaging
Added parameter to CBPRINTF_STATIC_PACKAGE which indicates buffer
alignment offset compared to CBPRINTF_PACKAGE_ALIGNMENT. When offset
is set to 0, macro assumes that input buffer is aligned to
CBPRINTF_PACKAGE_ALIGNMENT. When offset is positive, macro assumes
that buffer address is shifted by given number of bytes to
CBPRINTF_PACKAGE_ALIGNMENT alignment.

Extended cbprintf_package to use len argument as alignment offset
indicator when calculating length only (package pointer is null).

Features are not available for xtensa platform which seems to
require 16 byte alignment from the package. It is only an assumption
due to lack of the documentation and may be fixed in the future.

Feature allows to avoid unnecessary padding when package is part of
a message and preceeded by a header of a known size. For example,
message header on 32 bit architecture has 12 bytes, long doubles are
not used so cbprintf requires 8 byte alignment. Without alignment
offset indicator, package containing just a string with one argument
would need 4 byte padding after the header and 4 byte padding after
the package. Message would be 32 bytes long. With alignment offset
indication both paddings are not needed and message is only 24 bytes
long.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2021-03-29 14:08:41 +02:00
Anas Nashif
f5a1be80cd libc: strncat: match implementation to declaration
The identifiers used in the declaration and definition of a function
shall be identical [MISRAC2012-RULE_8_3-b]

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-29 07:52:42 -04:00
Anas Nashif
841d6f3ba6 libc: malloc: match implementation to declaration
The identifiers used in the declaration and definition of a function
shall be identical [MISRAC2012-RULE_8_3-b]

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-29 07:52:42 -04:00
Anas Nashif
05d7d32122 posix: timer: match implementation to declaration
The identifiers used in the declaration and definition of a function
shall be identical [MISRAC2012-RULE_8_3-b]

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-29 07:52:42 -04:00
Anas Nashif
7c8d932e65 libc: gmtime_r: match implementation to declaration
The identifiers used in the declaration and definition of a function
shall be identical [MISRAC2012-RULE_8_3-b]

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-29 07:52:42 -04:00
Anas Nashif
8415c94ab1 libc: printf/..: match implementation to declaration
The identifiers used in the declaration and definition of a function
shall be identical [MISRAC2012-RULE_8_3-b]

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-29 07:52:42 -04:00
Anas Nashif
27af48417e libc: snprintf/..: match implementation to declaration
The identifiers used in the declaration and definition of a function
shall be identical [MISRAC2012-RULE_8_3-b]

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-29 07:52:42 -04:00
Anas Nashif
0576a3d0e6 libc: fputs/puts: match implementation to declaration
The identifiers used in the declaration and definition of a function
shall be identical [MISRAC2012-RULE_8_3-b]

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-29 07:52:42 -04:00
Anas Nashif
4f52519c7e libc: strtol/strtoul: match implementation to declaration
The identifiers used in the declaration and definition of a function
shall be identical [MISRAC2012-RULE_8_3-b]

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-29 07:52:42 -04:00
Flavio Ceolin
9fd4ea91b7 coccinelle: Remove extra semicolon
coccicheck --mode=patch --cocci=semicolon.cocci

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-03-25 11:35:30 -05:00
Anas Nashif
52775ff263 kernel: rename reserved 'exp' symbol
This symbol is reserved and usage of reserved symbols violates the
coding guidelines. (MISRA 21.2)

NAME
       exp, expf, expl - base-e exponential function

SYNOPSIS
       #include <math.h>

       double exp(double x);

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-25 07:28:37 -04:00
Anas Nashif
1b6933d231 kernel: heap: rename 'free' and 'alloc'
This symbol is reserved and usage of reserved symbols violates the
coding guidelines. (MISRA 21.2)

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-25 07:28:37 -04:00
Anas Nashif
21d3225317 kernel: heap: rename resereved 'rewind'
This symbol is reserved and usage of reserved symbols violates the
coding guidelines. (MISRA 21.2)

NAME
   fgetpos, fseek, fsetpos, ftell, rewind - reposition a stream

SYNOPSIS
   #include <stdio.h>

   void rewind(FILE *stream);

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-25 07:28:37 -04:00
Anas Nashif
581214524a ring_buffer: rename resereved 'rewind' symbol
This symbol is reserved and usage of reserved symbols violates the
coding guidelines. (MISRA 21.2)

NAME
       fgetpos, fseek, fsetpos, ftell, rewind - reposition a stream

SYNOPSIS
       #include <stdio.h>

       void rewind(FILE *stream);

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-25 07:28:37 -04:00
Guðni Már Gilbert
952970d6cb lib: libc: minimal: add check on returned value
Fix #32938 [Coverity CID :219508] "Unchecked return value in
lib/libc/minimal/source/stdlib/malloc.c"

The Coverity complains about sys_mutex_lock() which returns 0 if
locked. I added also the same check on returned value for
sys_mutex_unlock() which returns 0 if unlocked.

Signed-off-by: Guðni Már Gilbert <gudni.m.g@gmail.com>
2021-03-23 12:13:00 +01:00
Anas Nashif
0ac3073d20 fdtable: fdtable: do not use tag name fd_entry
Do not reuse tag name (misra rule 5.7).

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-22 19:48:14 -04:00
Anas Nashif
5c90ceb105 clock: rename z_tick_get_32 -> sys_clock_tick_get_32
Do not use z_ for internal APIs, z_ is for private APIs within one
subsystem only.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-03-19 11:22:17 -04:00
Marcin Niestroj
064c6ef830 libc: minimal: implement time() API
Implement time() API by using clock_gettime(CLOCK_REALTIME, ...).

Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
2021-03-19 09:56:53 -04:00
Nicolas Pitre
b1eefc0c26 lib/os/heap: straighten up our type usage
The size_t usage, especially in struct z_heap_bucket made the heap
header almost 2x bigger than it needs to be on 64-bit systems.
This prompted me to clean up our type usage to make the code more
efficient and easier to understand. From now on:

- chunkid_t is for absolute chunk position measured in chunk units
- chunksz_t is for chunk sizes measured in chunk units
- size_t is for buffer sizes measured in bytes

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-03-18 19:33:39 -04:00
Nicolas Pitre
e919bb2d16 lib/os/heap: abstract conversion from chunk size to usable bytes
This is the reverse of bytes_to_chunksz().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-03-18 19:33:39 -04:00
Nicolas Pitre
f4e1611692 lib/os/heap: no need to over-mask
bmask already represents the intersection of h->avail_buckets and
wanted buckets.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-03-18 19:33:39 -04:00
Nicolas Pitre
a54e101a1e lib/os/heap: rename struct z_heap.len to struct z_heap.end_chunk
The end marker chunk was represented by the len field of struct z_heap.
It is now renamed to end_chunk to make it more obvious what it is.

And while at it...

Given that it is used in size_too_big() to cap the allocation size
already, we no longer need to test the bucket index against the
biggest index possible derived from end_chunk in alloc_chunk(). The
corresponding bucket_idx() call is relatively expensive on some
architectures so avoiding it (turning it into a CHECK() instead) is
a good thing.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-03-18 19:33:39 -04:00
Nicolas Pitre
e8910f629d lib/os/heap: document the reason behind the header field ordering
This is not obvious why the order is important when only looking at
the header file.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-03-18 19:33:39 -04:00
Nicolas Pitre
f2fd0e8bb6 lib/os/heap: make printed heap info more useful
Turn sys_heap_dump() into sys_heap_print_info() to better reflect
what it actually does, and improve the information being printed.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-03-16 16:06:53 +01:00
Ningx Zhao
7874052df2 lib/rbtree: Remove dead case in rb_remove()
This "else" clause was dead code, in a valid
tree it's not possible to have a node and
its child both be red.
Fix issue #33239.

Signed-off-by: Ningx Zhao <ningx.zhao@intel.com>
2021-03-13 06:49:08 -05:00
Krzysztof Chruscinski
9966d85c0a lib: os: cbprintf: Add alignment check to cbprintf_package
Added validation of alignment to cbprintf_package. Error is returned if
input buffer is not aligned to the largest argument.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2021-03-11 09:54:39 +01:00
Krzysztof Chruscinski
47a565367e lib: os: cbprintf: Add static packaging
Extend cbprintf with static packaging.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2021-03-11 09:54:39 +01:00
Guðni Már Gilbert
94f88e502f lib: os: Simplify z_impl_z_sys_mutex_kernel_unlock
Remove one redundant if() statement already included
with k_mutex_unlock()

Signed-off-by: Guðni Már Gilbert <gudni.m.g@gmail.com>
2021-03-10 05:42:06 -05:00
Nicolas Pitre
14e5e98822 lib: cbprintf: add support for deferred formatting
In applications like logging the call site where arguments to
formatting are available may not be suitable for performing the
formatting, e.g. when the output operation can sleep.  Add API that
supports capturing data that may be transient into a buffer that can
be saved, and API that then produces the output later using the
packaged arguments.

[ Documentation and commit log from Peter Bigot. ]

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-03-05 09:29:35 +01:00
Spoorthy Priya Yerabolu
6d10db3ed6 Coding guideline: Fixing code violations for 21.13 Rule
Any value passed to a function in <ctype.h> shall be
representable as an unsigned char or be the value EOF.

So changed type of variable to unsigned char.

Signed-off-by: Spoorthy Priya Yerabolu <spoorthy.priya.yerabolu@intel.com>
2021-03-05 10:53:13 +03:00
Peter Bigot
b706a5e999 kernel: remove old work queue implementation
Now that the old API has been reimplemented with the new API remove
the old implementation and its tests.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-03-03 20:06:00 -05:00
Peter Bigot
4e3b92609b kernel: provide functional equivalent to old userspace work queue API
The new API cannot be used from userspace because it is not merely a
wrapper around existing userspace-capable objects (threads and
queues), but instead requires much more complex and lower-level access
to memory that can't be touched from userspace.  The vast majority of
work queue users are operating from privileged mode, so there's little
motivation to go through the pain and complexity of converting all
functions to system calls.

Copy the necessary pieces of the existing userspace work queue API out
and expose them with new names and types:

* k_work_handler_t becomes k_work_user_handler_t
* k_work becomes k_work_user
* k_work_q becomes k_work_user_q

etc.  Because the replacement API cannot use the same types new API
names are also introduced to make it more clear that the userspace
work queue API is a separate functionality.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-03-03 20:06:00 -05:00
Peter Bigot
44539ed645 kernel: select work queue implementation
Attempts to reimplement the existing work API using a new work
implementation failed, primarily due to heavy use of whitebox testing
in validating the original API.  Add a temporary Kconfig that will
select between the two implementations so we can use the same
identifiers but select which implementation they reference.

This commit just adds the selection infrastructure and uses it to
conditionalize the existing implementation in anticipation of the new
one in the next commit.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-03-03 20:06:00 -05:00
James Harris
c7bb423f3e kernel: fix race conditions with z_ready_thread
Several internal APIs wrote thread attributes (return value, mainly)
_after_ calling `z_ready_thread`. This is unsafe, at least in SMP,
because another core could have already picked up and run the thread.

Fixes #32800.

Signed-off-by: James Harris <james.harris@intel.com>
2021-03-03 13:54:47 -05:00
Rajavardhan Gundi
4269ecd2bc lib: os: Introduce support for CRC32C algorithm
This introduces the support for CRC32C (Castagnoli) algorithm.
The generator polynomial used is 0x1EDC6F41UL.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2021-03-02 14:08:30 +01:00
Jakub Rzeszutko
ddb07f57ec lib: add freebsd getopt library
This library is going to be used by the shell module. Some shell users
are not satisfied with subcommands alone and need to use the options
for commands as well.

Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@nordicsemi.no>
2021-03-01 09:50:32 -05:00
Jakub Rzeszutko
72dd5a5c68 lib: move fnmatch to lib/util
We expect to have more libraries with incopatible license. There must
be a common place for such software. It seems that lib/util is good
place for that.

Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@nordicsemi.no>
2021-03-01 09:50:32 -05:00
Nicolas Pitre
fb73ac392c lib/os/cbprintf_nano.c: several improvements
This makes cbprintf_nano.c much closer to the standard printf and
therefore more useful. The following are now implemented:

- right justification for everything (only for numbers previously)
- precision value for numbers, chars and strings
- width/precision passed as arguments with *
- "unlimited" padding length
- lower/uppercase hex output
- the #, + and ' ' flags are supported

And the code was heavily reworked to reduce its size as much as
possible to mitigate the size growth. Still, the binary resulting
from cbprintf_nano.c is now between 10% and 20% bigger depending on
the architecture. This is still far smaller than cbprintf_complete.c
which remains about twice as big on average even without FP support.

Many unit tests that were skipped with CONFIG_CBPRINTF_NANO are now
enabled, and a few more were added for good measure.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-02-23 19:39:59 +01:00
Nicolas Pitre
7a91cf0176 Revert "lib/os/heap: introduce option to force big heap mode"
This reverts commit b6b6d39bb6.

With both commit 4690b8d5ec ("libc/minimal: fix malloc() allocated
memory alignment") and commit c822e0abbd ("libc/minimal: fix
realloc() allocated memory alignment") in place, there is no longer
a need for enforcing the big heap mode on every allocations.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-02-19 07:32:22 -05:00
Andy Ross
9a594a0c1a lib/p4wq: Fix race with completed work items
Work items can be legally resubmitted from within their own handler.
Currently the p4wq detects this case by checking their thread field to
see if it's been set to NULL.  But that's a race, because if the item
was NOT resubmitted then it no longer belongs to the queue and may
have been freed or reused or otherwise clobbered legally by user code.

Instead, steal a single bit in the thread struct for this purpose.
This patch adds a K_CALLBACK_STATE bit in user_options and documents
it in such a way (as being intended for "callback manager" utilities)
that it can't be used recursively or otherwise collide.

Fixes #32052

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2021-02-14 18:17:28 -05:00
Anas Nashif
5d1c535fc8 license: add missing SPDX headers
Add SPDX header to files with existing license.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-02-11 08:05:16 -05:00
Nicolas Pitre
e539bb1979 libc/minimal: restore proper locking for malloc() and friends
Commit 40016a6a92 ("libc/minimal: Use a sys_heap for the malloc
implementation") replaced sys_mem_pool_alloc() with sys_heap_alloc().
The problem is that those aren't equivalent. While the former did
guard against concurrent usage, the later doesn't.

Add the same locking around sys_heap_alloc() that used to be implicit
with sys_mem_pool_alloc().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-02-08 12:40:16 -05:00
Andrzej Puzdrowski
68a5ec3cb2 lib/gui/lvgl: Add fs_dir_t type variable initialization …
The commit adds initialization of fs_dir_t variables in preparation
for fs_opendir function change that will require fs_dir_t object, passed
to the function, to be initialized before first usage.

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
2021-02-05 07:32:20 -05:00
Andrzej Puzdrowski
8339da6da6 lib/posix/fs: Add fs_dir_t type variable initialization
The commit adds initialization of fs_dir_t variables in preparation
for fs_opendir function change that will require fs_dir_t object, passed
to the function, to be initialized before first usage.

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
2021-02-05 07:32:20 -05:00
Nicolas Pitre
47de5a0591 libc/minimal: fix realloc() failure case
It is said that the C17 realloc() behavior is to return the original
pointer on error and that's what is implemented here. This may be
confused with a successful realloc() and nobody else does that.

Instead, a failed realloc() should return NULL, leave the original
memory intact and set errno to ENOMEM. This is the behavior described
by all the following references:

Linux/glibc:
https://man7.org/linux/man-pages/man3/malloc.3.html

NetBSD
https://man.netbsd.org/realloc.3

Microsoft
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/realloc

Mac OS X
https://developer.apple.com/library/archive/documentation/System/\
Conceptual/ManPages_iPhoneOS/man3/reallocf.3.html

Open Group Base Specifications Issue 6
https://pubs.opengroup.org/onlinepubs/009604599/functions/realloc.html

PTC MKS Toolkit
https://www.mkssoftware.com/docs/man3/realloc.3.asp

Let's get in line with the most common behavior.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-02-02 19:08:24 -05:00
Nicolas Pitre
593997046b lib/os/heap: fix out-of-bounds usage of memcpy() in sys_heap_realloc()
The sys_heap_realloc() code falls back to allocating new memory
and copying the existing data over when it cannot adjust the size
in place. However the size of the data to copy should be the old
size and not the new size if we're extending the allocation.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-02-02 19:08:24 -05:00
Nicolas Pitre
c822e0abbd libc/minimal: fix realloc() allocated memory alignment
The definition for realloc() says that it should return a pointer
to the allocated memory which is suitably aligned for any built-in
type.

Turn sys_heap_realloc() into a sys_heap_aligned_realloc() and use it
with __alignof__(z_max_align_t) to implement realloc() with proper
memory alignment for any platform.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-02-02 19:08:24 -05:00
Nicolas Pitre
4690b8d5ec libc/minimal: fix malloc() allocated memory alignment
The definition for malloc() says that it should return a pointer
to the allocated memory which is suitably aligned for any built-in
type. This requirement was lost in commit 0c15627cc1 ("lib: Remove
sys_mem_pool implementation") where the entire memory pool used to
have an explicit alignment of 16.

Fix this by allocating memory with sys_heap_aligned_alloc() using
__alignof__(z_max_align_t) which will automatically get the needed
alignment on each platform.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-02-02 19:08:24 -05:00
Dominik Ermel
ba3b1ba3d5 posix: Add fs_file_t type variable initializations
The commit adds initializations of fs_file_t variables in preparation
for fs_open function change that will require fs_file_t object, passed
to the function, to be initialized before first usage.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2021-01-29 08:04:51 -05:00
Peter Bigot
10180af38d lib: cbprintf: avoid referencing distinct union fields in a statement
An assignment from one multi-word union field to another was not safe
from corruption.  Copy the value out to a local value before storing it
to the preferred union field.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-01-27 16:45:38 -05:00
Peter Bigot
8528e45897 lib: cbprintf: add libc f/printf substitutes
This allows applications that may not use minimal libc avoid the cost
of a second printf-like formatting infrastructure by using printfcb()
instead of printf() for output.  It also helps make sure that the
formatting support (e.g. floats) is consistent between user-directed
output and the logging infrastructure.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-01-27 13:34:06 -05:00
Peter Bigot
98747abf9c libc: minimal: add putchar
Provide putc and putchar wrappers around fputc.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-01-27 13:34:06 -05:00
Klaus H. Sorensen
4e7b5769bd lib: crc32_sw: 4 bit at a time implementation
Calculate crc32 4 bits at a time. The return value of the calculation is
identical to the previous 1 bit at a time implementation.

Results in a speed up of a factor 3 at the cost of using 64 bytes of
flash for a crc table.

Calculating crc32 of 128kB of flash on a 120MHz Kinetis MKE16F512
Cortex-M4 takes 99ms using the 1 bit at a time implementation, and 30ms
using the 4 bits at a time implementation.

The crc32 routine is used by subsys/canbus/canopen/canopen_program.c to
calculate crc of flash images.

Signed-off-by: Klaus H. Sorensen <khso@vestas.com>
2021-01-24 14:24:48 -05:00
Martin Åberg
b6b6d39bb6 lib/os/heap: introduce option to force big heap mode
This option allows forcing big heap mode. Useful on for getting 8-byte
aligned blocks on 32-bit machines.

Signed-off-by: Martin Åberg <martin.aberg@gaisler.com>
2021-01-24 10:11:11 -05:00
Martin Åberg
180ce491ad libc/minimal: reallocarray() in terms of realloc()
reallocarray() is defined in terms of realloc(). From OpenBSD manual
pages:
    "Designed for safe allocation of arrays, the reallocarray()
    function is similar to realloc() except it operates on nmemb
    members of size size and checks for integer overflow in the
    calculation nmemb * size."

The return value of sys_heap_realloc() is not compatible with that
of realloc().

Signed-off-by: Martin Åberg <martin.aberg@gaisler.com>
2021-01-24 10:11:11 -05:00
Andrew Boie
e2b62b8abb newlib: clamp max heap size on MMU systems
Previously, newlib claimed all free physical memory in the
system.

Now, the kernel manages this, allowing for memory to be
used via k_mem_map() calls.

Establish an upper bound to how much newlib will try to
claim on system startup, instead of trying to take all
of it, allowing other parts of the system to also map
anonymous memory.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2021-01-23 19:47:23 -05:00
Andrew Boie
b01177156f newlib: memory-map the heap, cleanups
We now draw heap memory from an anonymous memory mapping
instead of a hard-coded region past the kernel image,
which is no longer mapped by default.

Some readability cleanups were made to a particuarly
horrible set of nested ifdefs. A few types were adjusted.

sbrk()'s count argument is an intptr_t, not an int.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2021-01-23 19:47:23 -05:00
Nicholas Lowell
ce480e8577 posix: clock: nanosecond resolution
directly convert ticks to nsecs in the clock_* posix
functions which will provide the best resolution the
system allows

Signed-off-by: Nicholas Lowell <nlowell@lexmark.com>
2021-01-22 12:37:58 -05:00
Nicolas Pitre
a2011d8af9 z_heap_aligned_alloc(): avoid memory wastage
The strategy used in z_heap_aligned_alloc() was to allocate an extra
align-sized memory block for storing a pointer to the memory heap.
This is wasteful in terms of memory usage when alignment is larger
than a pointer width. A loop is needed to find the initial memory
start when freeing it which isn't optimal either.

Instead, let's have sys_heap_aligned_alloc() rewind a pointer after
it is aligned to make just enough room for storing our heap reference.
This way the heap reference is always located immediately before the
aligned memory and any unused memory is returned to the heap.

The rewind and alignment values may coincide in which case only
the alignment is necessary anyway.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-01-22 10:04:43 -05:00
Anas Nashif
ee599d88c3 Revert "newlib: memory-map the heap, cleanups"
This reverts commit bf91269738.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-01-22 08:39:45 -05:00
Anas Nashif
808303c5f4 Revert "newlib: clamp max heap size on MMU systems"
This reverts commit e966244de2.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-01-22 08:39:45 -05:00
Andrew Boie
e966244de2 newlib: clamp max heap size on MMU systems
Previously, newlib claimed all free physical memory in the
system.

Now, the kernel manages this, allowing for memory to be
used via k_mem_map() calls.

Establish an upper bound to how much newlib will try to
claim on system startup, instead of trying to take all
of it, allowing other parts of the system to also map
anonymous memory.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2021-01-21 16:47:00 -05:00
Andrew Boie
bf91269738 newlib: memory-map the heap, cleanups
We now draw heap memory from an anonymous memory mapping
instead of a hard-coded region past the kernel image,
which is no longer mapped by default.

Some readability cleanups were made to a particuarly
horrible set of nested ifdefs. A few types were adjusted.

sbrk()'s count argument is an intptr_t, not an int.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2021-01-21 16:47:00 -05:00
Peter Bigot
ee7c9f7fe8 libc: minimal: add INTn_C macros to stdint.h
Macros like INT64_C(x) convert x to a constant integral expression,
i.e. one that can be used in preprocessor code.  Implement wrappers
that use the GNUC intrinsics to perform the translation.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-01-21 14:50:01 -05:00
Rohit Gujarathi
f023df581f drivers: display: Added support for restricted update.
Added support for restricted updating of monochrome
displays.

Signed-off-by: Rohit Gujarathi <gujju.rohit@gmail.com>
2021-01-21 17:26:37 +01:00
Peter A. Bigot
f6d7595d4e lib/timeutil: add utilities to manage local/reference clock alignment
Provide data structures to capture a timestamp in two different
clocks, monitor the drift between those clocks, and using a base
instant with estimated drift convert between the clocks.

This provides the core technology to convert between system uptime and
an external continuous time scale like TAI (UTC without applying leap
seconds).

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2021-01-20 16:38:56 -05:00
Nicolas Pitre
f436315e02 lib/os/heap: guard against arithmetic overflows
Let's do it upfront only once for each entry point and dispense
with overflow checks later to keep the code simple.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-01-20 14:15:44 -05:00
Torsten Rasmussen
da3726006b cmake: newlib linking flags -lc and -lgcc circular dependency
Fixes: #28650

Linking with newlib now defines the following linker flags as:
```
${CMAKE_C_LINKER_WRAPPER_FLAG}${CMAKE_LINK_LIBRARY_FLAG}c
${CMAKE_C_LINKER_WRAPPER_FLAG}${CMAKE_LINK_LIBRARY_FLAG}gcc
c
```

This is needed because when linking with newlib on aarch64, then libgcc
has a link dependency to libc (strchr), but libc also has dependencies
to libgcc.

CMake is capable of handling circular link dependencies for CMake
defined static libraries, which can be further controlled using
`LINK_INTERFACE_MULTIPLICITY`.

However, libc and libgcc are not regular CMake libraries, and is seen as
linker flags by CMake, and thus symbol de-duplications will be
performed.

CMake link options cannot be used, as that will place those libs first
on the linker invocation. -Wl,--start-group is problematic as the
placement of -lc and -lgcc is not guaranteed in case later libraries are
also using -lc / -libbgcc as interface linker flags.

Thus, we resort to use
`${CMAKE_C_LINKER_WRAPPER_FLAG}${CMAKE_LINK_LIBRARY_FLAG}`
as this ensures the uniqueness and thus avoids symbol de-duplication
which means libc will be followed by libgcc, which is finally followed
by libc again.

It would have been possible to use `-lc` directly, but there is a risk
that an externally library is also adding `-lc` and thus de-duplication
and re-arrangement of this flag happens. This risk is in theory also
existing with this fix, but the long nature of this link flag with using
`${CMAKE_C_LINKER_WRAPPER_FLAG}` would likely indicate a similar fix and
thus those libraries will stay in order.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2021-01-20 09:25:38 -05:00
Eugeniy Paltsev
74c4d5ae2a libc: minimal: stdout: fix fputs return value
The 'fputs' has flaw in the implementation. It almost always
returns 'EOF' even if completed successfully.
This happens because we compare 'fwrite' return value which is
"number of members successfully written" (which is 1 in current
implementation) to the total string size:

----------------------------->8-----------------------
int fputs(const char *_MLIBC_RESTRICT string,
          FILE *_MLIBC_RESTRICT stream)
{
	int len = strlen(string);
	int ret;

	ret = fwrite(string, len, 1, stream);

	return len == ret ? 0 : EOF;
}
----------------------------->8-----------------------

In result 'fputs' return 'EOF' in case of string length bigger
than 1.

There are several fixes possible, and one of the fixes is to
swap number of items (1) with size (string length) when we
are calling 'fwrite'. The only difference will be that
'fwrite' will return actual numbers of bytes written which
can be compared with the string length.

Signed-off-by: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
2021-01-19 23:03:12 -05:00
Ningx Zhao
36d83a5156 tests: ringbuffer: code coverage improvements
Add double macros to remove the __week function
when calculating coverage.

Signed-off-by: Ningx Zhao <ningx.zhao@intel.com>
2021-01-18 23:12:47 -05:00
Nicolas Pitre
7a22a4bdf6 heap: clean up some size related issues
First, the maximum heap size must fit in 31 bits worth of chunks
because the internal 32-bit field holding the size is shared with
the `used` bit.

Then the mention of a 256-byte block in the doc is no longer
relevant. That pertained to the previous allocator implementation.

And ditto for the HEAP_MEM_POOL_MIN_SIZE kconfig option.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-01-15 12:08:20 -05:00
Andy Ross
d2eadfa162 lib/os: P4 Work Queue: Pooled Parallel Preemptible Priority-based
This adds a somewhat special purpose IPC mechanism.  It's intended for
applications which have a "work queue" like architecture of discrete
callback items, but which need the ability to schedule those items
independently in separate threads across multiple CPUs.  So P4 Work
items:

1. Can run at any Zephyr scheduler priority and with any deadline
   (this feature assumes EDF scheduling is enabled)

2. Can be submitted at any time and from any context, including being
   resubmitted from within their own handler.

3. Will preempt any lower priority work as soon as they are runnable,
   according to the standard rules of Zephyr priority scheduling.

4. Run from a pool of worker threads that can be allocated efficiently
   (i.e. you need as many as the number of CPUs plus the number of
   preempted in-progress items, but no more).

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2021-01-15 11:35:50 -05:00
Peter Bigot
6297c667c5 lib: os: cbprintf: correct arg extraction of wide characters
The l length modifier can apply to the c format specifier; in that
case the expected value is of type wint_t.  Minimal libc doesn't
define wint_t, and it is complex to do so correctly (must add
<wchar.h>, and use a lot of conditional tricks).

wint_t can differ from wchar_t in rank when wchar_t undergoes default
integral promotion, which it does on xtensa (wchar_t is unsigned
short).  So we can use wchar_t as an approximation, except in va_arg
where we need to use a wider type: int covers this case.

Note that we still don't format wide characters, but we do want to
consume the correct amount of data for a default-promoted extended
character.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-01-07 14:02:06 +01:00
Peter Bigot
8eda19a898 lib: os: cbprintf: correctly handle signed vs unsigned char
Whether char is signed or unsigned is toolchain and target specific.
Rather than assume it's signed (which is true for x86, but not for
ARM), do the right thing based on whether the minimum representable
value is less than zero.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-01-07 14:02:06 +01:00
Peter Bigot
b036afba5a lib: cbprintf: document length modifiers
It may not be clear that the length modifiers reference native C types
with specific ranks.  Document the core type for each modifier.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-01-07 14:02:06 +01:00
Peter Bigot
d12a99588b lib: cbprintf: remove cbprintf_arglen
This function was designed to support the logging infrastructure's
need to copy values from va_list structures.  It did not meet that
need, since some values need to be changed based on additional data
that is only available when the complete format specification is
examined.  Remove the function as unnecessary.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-01-07 14:02:06 +01:00
Peter Bigot
0bc01822a2 lib: cbprintf: improve coverage
Providing a literal width or precision that exceeds the non-negative
range of int does not appear to be rejected by the standard, but it
does produce a build diagnostic so we can't test it.  Switch to an
equivalent form that doesn't affect line coverage.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-01-07 14:02:06 +01:00
Peter Bigot
9d14f72593 lib: libc: minimal: remove outdated comment
The referenced functions may not have been implemented back in 2015,
but they are now.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-01-04 20:39:16 -05:00
Nicolas Pitre
36f075847c lib/os/heap: always assert the align value argument
Just like commit 0ae04f01b6 ("lib/os/heap: make some checks more
assertive") we shouldn't validate the externally provided align
argument only when CONFIG_SYS_HEAP_VALIDATE is set.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-01-04 16:57:47 -08:00
Nicolas Pitre
aeda6ccdd8 lib/os/heap: fix realloc issues
If the new size amounts to the same number of chunks then:

- If right-chunk is used then we needlessly allocate new memory and
  copy data over.

- If right-chunk is free then we attempt to split it with a zero size
  which corrupts the prev/next list.

Make sure this case is properly handled and add a test for it.

While at it, let's simplify the code somewhat as well.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-01-04 11:12:05 -05:00
Nicolas Pitre
16fa1cf844 lib/os/heap: add missing realloc semantics
Need to handle those cases where a NULL pointer and/or 0 size
is provided.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-12-18 14:07:09 -05:00
Peter Bigot
53762239c1 lib: cbprintf: fix mishandling of precision string output
If a precision flag is included for s formatting that bounds the
maximum output length, so we need to use strnlen rather than strlen to
get the amount of data to emit.  With that flag we can't expect there
to be a terminating NUL following the text to print.

Also fix handling of an empty precision, which should behave as if a
precision of zero was provided.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-12-18 08:23:24 -05:00
Nicolas Pitre
cf6fb4dea2 lib: cbprintf: float conversion optimization and documentation
While documenting the float conversion code, I found there was room
for some optimization. In doing so I added test cases to cover edge
cases e.g. making sure proper rounding is applied and that no loss
of precision was introduced. Compiled code should be smaller and
faster.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-12-08 14:09:15 -05:00
Andy Ross
fcd392f6ce kernel: subsys: lib: drivers: Use k_heap instead of z_mem_pool wrappers
Use the core k_heap API pervasively within our tree instead of the
z_mem_pool wrapper that provided compatibility with the older mempool
implementation.

Almost all of this is straightforward swapping of one alloc/free call
for another.  In a few cases where code was holding onto an old-style
"mem_block" a local compatibility struct with a single field has been
swapped in to keep the invasiveness of the changes down.

Note that not all the relevant changes in this patch have in-tree test
coverage, though I validated that it all builds.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-12-07 21:50:14 -05:00
Andy Ross
0c15627cc1 lib: Remove sys_mem_pool implementation
This has been replaced by sys_heap now and all dependencies are gone.
Remove.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-12-07 21:50:14 -05:00
Andy Ross
40016a6a92 libc/minimal: Use a sys_heap for the malloc implementation
The older sys_mem_pool is going away and being replaced by a new
allocator.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-12-07 21:50:14 -05:00
Andy Ross
40c1b55cc2 lib/os/heap: Add sys_heap_realloc()
Add an optimized realloc() implementation that can successfully expand
allocations in place if there exists enough free memory after the
supplied block.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-12-07 21:50:14 -05:00
Andy Ross
6bc6a67b0e lib/gui/lvgl: Use a sys_heap and not a sys_mem_pool
The sys_mem_pool allocator is a legacy thing.  Use the standard heap
to reduce code size.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-12-07 21:50:14 -05:00
Andy Ross
6965cf526d kernel: Deprecate k_mem_pool APIs
Mark all k_mem_pool APIs deprecated for future code.  Remaining
internal usage now uses equivalent "z_mem_pool" symbols instead.

Fixes #24358

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-12-07 21:50:14 -05:00
Nicolas Pitre
9dcd76a650 lib/os/heap: minimize initial overallocation in the aligned case
The biggest required padding is equal to `align - chunk_header_bytes`
and not `align - 1` given that the header already contributes to the
padding.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-11-30 14:50:07 -05:00
Krzysztof Chruscinski
3ed8083dc1 kernel: Cleanup logger setup in kernel files
Most of kernel files where declaring os module without providing
log level. Because of that default log level was used instead of
CONFIG_KERNEL_LOG_LEVEL.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2020-11-27 09:56:34 -05:00
Peter Bigot
c1b0cf8ec6 lib: cbprintf: work around LLVM code generation bug
LLVM building for qemu_x86 appears to have an optimization bug where a
union that is assigned to hold values read from va_args() is inferred
to be a constant value, so is placed in ROM with an all-zero content.

Prevent this by packing the conversion state and the value union into
a single container structure that's stack allocated.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-11-19 12:37:33 +01:00
Robin-Charles Guihéneuf
f0250d5c71 lvgl: kscan: Only process prev state if it has been updated
When no message was get from kscan_msgq queue, the prev state
(x/y point) was processed as a new state. During process its
coordinates could be inverted or modified, depending on the
LVGL_POINTER_KSCAN_SWAP_XY, LVGL_POINTER_KSCAN_INVERT_X,
LVGL_POINTER_KSCAN_INVERT_Y or display orientation configuration.
In these cases, it could cause wrong input data.

Signed-off-by: Robin-Charles Guihéneuf <robin-charles@hotmail.fr>
2020-11-19 12:36:31 +01:00
Anas Nashif
43989ca675 libc: add labs() and llabs()
Add support for abs with additional integer types.

This is needed to make LLVM quiet and stop warning about abs being used
with int64_t and such.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2020-11-19 08:41:56 +01:00
Peter Bigot
a9e2b10a86 lib: cbprintf: ignore l length modifier on float values
%lf is specified to be the same as %f, and should not be marked as
 invalid.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-11-17 17:49:22 -05:00
Marcin Niestroj
0d946b712b libc: minimal: print stderr just like stdout
So far data that went to stderr was simply dropped in case of minimal
libc. In case of newlib stderr was treated same like stdout
(e.g. fprintf(stderr, ...) was equivalent to fprintf(stdout, ...).

Extend filter on stream pointer to allow both stdout and stderr to pass
data to stdout hook (which is Zephyr console backend in most cases).

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
2020-11-17 12:29:19 -08:00
Peter Bigot
607b390ba4 lib: cbprintf: fix pointer justification and padding
Although flags with pointers are not defined behavior, there is a
desire to have them work, so add a test and fix the complete
implementation so it passes.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-11-17 11:34:25 +01:00
Nicolas Pitre
d583af7707 lib/os/cbprintf_nano.c: simplifications and enhancements
Simplify the code to increase readability, and fix right-padding
for %p.

Also, the compiled code is smaller with those changes applied.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-11-17 11:34:25 +01:00
Martin Åberg
9feb799762 lib: cbprintf: improved %p specifier handling
This fixes an issue where the %p specifier always generated "(nil)"
on SPARC. The failing test cases were:
 tests/lib/sprintf/libraries.libc.sprintf
 tests/kernel/common/kernel.common.misra
 tests/kernel/common/kernel.common.tls
 tests/kernel/common/kernel.common

The exact logic behind the issue has not been fully analyzed, but
it can be observed that this commit eliminates one occurrence of
undefined behavior. (Only allowed to read the last union field written.)

Signed-off-by: Martin Åberg <martin.aberg@gaisler.com>
2020-11-13 14:53:55 -08:00
Martin Åberg
73b7dde833 cmsis: align buffer containing struct k_timer
The slab cmis_timer_slab is used to allocate objects of the type
"struct timer_obj", so it should be defined to use the compile-time
alignment requirement of "struct timer_obj", rather than 4.

Signed-off-by: Martin Åberg <martin.aberg@gaisler.com>
2020-11-13 14:53:55 -08:00
Martin Åberg
6f4bcf7933 libc: minimal: Add SPARC support
This commit defines the off_t type for SPARC.

Signed-off-by: Martin Åberg <martin.aberg@gaisler.com>
2020-11-13 14:53:55 -08:00
Martin Åberg
6d126e7481 lib/posix: SPARC newlib has unsigned short mode_t
This commit eliminates a compilation error by passing int to va_arg
rather than mode_t on SPARC.

Newlib sys/_types.h defines mode_t for SPARC as:
  typedef unsigned short __mode_t;

GCC 10.2.0 gave the following error message and suggested solution:

mqueue.c: In function 'mq_open':
mqueue.c:61:21: error: 'mode_t' {aka 'short unsigned int'} is promoted
 to 'int' when passed through '...' [-Werror]
   61 |   mode = va_arg(va, mode_t);
      |                     ^
mqueue.c:61:21: note: (so you should pass 'int' not 'mode_t' {aka 'short
unsigned int'} to 'va_arg')

Signed-off-by: Martin Åberg <martin.aberg@gaisler.com>
2020-11-13 14:53:55 -08:00
Peter Bigot
bb99422c8a lib/os: replace z_vprintk with cbprintf
Using the same implementation as the rest of Zephyr reduces code size.

Update options and expected results for formatting test.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-11-13 06:38:01 -05:00
Peter Bigot
18851d8ef9 libc/minimal: remove z_prf
All in-tree uses have been replaced by cbprintf, and the API was
private so there should be no out-of-tree users.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-11-13 06:38:01 -05:00
Peter Bigot
7cf9a8c102 libc: switch to cbprintf as basis for printf functionality
The minimal libc provided by Zephyr can use the Zephyr system
implementation rather than have its own implementation.

When combined with CBPRINTF_NANO some sprintf tests must be
skipped as they assume a more capable libc.  Add an overlay
that supports testing this non-default combination.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-11-13 06:38:01 -05:00
Peter Bigot
33103828dc lib: add cbprintf capability
This commit adds a C99 stdio value formatter capability where
generated text is emitted through a callback.  This allows generation
of arbitrarily long output without a buffer, functionality that is
core to printk, logging, and other system and application needs.

The formatter supports most C99 specifications, excluding:
* %Lf long double conversion
* wide character output

Kconfig options allow disabling features like floating-point
conversion if they are not necessary.  By default most conversions are
enabled.

The original z_vprintk() implementation is adapted to meet the
interface requirements of cbvprintf, and made available as an opt-in
feature for space-constrained applications that do not need full
formatting support.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-11-13 06:38:01 -05:00
Peter Bigot
abb3a28c94 Revert "shell: support floating point output with newlib"
This reverts commit e812ee6c21.

This is the initial step towards replacing the core Zephyr formatting
infrastructure with a common functionally-complete solution.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-11-13 06:38:01 -05:00
Peter Bigot
74fd3a8143 libc: minimal: add maximum integer typedefs
Use 64-bit as the maximum integer size.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-11-13 06:38:01 -05:00
Krzysztof Chruscinski
1e46bb3bb5 lib: os: ring_buffer: Allow using full buffer capacity
Previously, ring buffer had capacity of provided buffer size - 1. This
trick was used to distinguish between empty and full states. It had one
drawback: ring buffer could not be used as a pool of equal sized buffers
(using ring_buf_put_claim and ring_buf_get_claim).
Reworked internals to use non wrapping head and tail. Since they are
non wrapping, there is no issue with distinguishing between empty and
full. Since this appraoch would be vulnerable to wrapping on 32 bit
boundary, added a mechanism which periodically reduces all indexes to
avoid 32 bit wrapping.

After this rework, buffer has one byte more capacity. Simple test shows
slight performance improvement.

Updated tests to reflect increased capacity and added test to check if
it is possible to continuesly allocated 2 buffers of half ring buffer
size.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2020-11-11 13:17:49 +01:00
Andrew Boie
a29b31d800 libc: newlib: fix virtual memory arena size
The calculations need to be from the RAM base virtual
address and not the physical address.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2020-11-09 20:19:13 -05:00
Nicolas Pitre
a5c53d255c lib/os/prf.c: let the compiler optimize _ldiv5() on 64-bit architectures
The compiler doesn't need help here.

For example, gcc creates this on Aarch64:

_ldiv5:
        ldr     x1, [x0]
        mov     x2, -3689348814741910324
        movk    x2, 0xcccd, lsl 0
        add     x1, x1, 2
        umulh   x1, x1, x2
        lsr     x1, x1, 2
        str     x1, [x0]
        ret

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-11-09 13:23:25 -08:00
Nicolas Pitre
822dfbd012 lib/os/prf.c: alternate implementation for _ldiv5()
The _ldiv5() is an optimized divide-by-5 function that is smaller and
faster than the generic libgcc implementation.

Yet it can be made even smaller and faster with this replacement
implementation based on a reciprocal multiplication plus some tricks.

For example, here's the assembly from the original code on ARM:

_ldiv5:
        ldr     r3, [r0]
        movw    ip, #52429
        ldr     r1, [r0, #4]
        movt    ip, 52428
        adds    r3, r3, #2
        push    {r4, r5, r6, r7, lr}
        mov     lr, #0
        adc     r1, r1, lr
        adds    r2, lr, lr
        umull   r7, r6, ip, r1
        lsr     r6, r6, #2
        adc     r7, r6, r6
        adds    r2, r2, r2
        adc     r7, r7, r7
        adds    r2, r2, lr
        adc     r7, r7, r6
        subs    r3, r3, r2
        sbc     r7, r1, r7
        lsr     r2, r3, #3
        orr     r2, r2, r7, lsl #29
        umull   r2, r1, ip, r2
        lsr     r2, r1, #2
        lsr     r7, r1, #31
        lsl     r1, r2, #3
        adds    r4, lr, r1
        adc     r5, r6, r7
        adds    r2, r1, r1
        adds    r2, r2, r2
        adds    r2, r2, r1
        subs    r2, r3, r2
        umull   r3, r2, ip, r2
        lsr     r2, r2, #2
        adds    r4, r4, r2
        adc     r5, r5, #0
        strd    r4, [r0]
        pop     {r4, r5, r6, r7, pc}

And here's the resulting assembly with this commit applied:

_ldiv5:
        push    {r4, r5, r6, r7}
        movw    r4, #13107
        ldr     r6, [r0]
        movt    r4, 13107
        ldr     r1, [r0, #4]
        mov     r3, #0
        umull   r6, r7, r6, r4
        add     r2, r4, r4, lsl #1
        umull   r4, r5, r1, r4
        adds    r1, r6, r2
        adc     r2, r7, r2
        adds    ip, r6, r4
        adc     r1, r7, r5
        adds    r2, ip, r2
        adc     r2, r1, r3
        adds    r2, r4, r2
        adc     r3, r5, r3
        strd    r2, [r0]
        pop     {r4, r5, r6, r7}
        bx      lr

So we're down to 20 instructions from 36 initially, with only 2 umull
instructions instead of 3, and slightly smaller stack footprint.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-11-09 13:23:25 -08:00
Jan Van Winkel
7c8ef52648 lib: gui: lvgl: update to v7.6.1
Update LVGL to minor release 7.6.1

resolves: #29050

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
2020-11-01 10:51:00 -05:00
Paul Sokolovsky
565228516c libc: minimal: Implement abort().
abort() is an important runtime function, oftentimes used to signal
abnormal execution conditions in generic applications. Worse, they
may be used under such circumstances in e.g. compiler support
libraries, in which case lack of implementation of this function
will lead to link error.

Fixes: #29541

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2020-10-28 17:02:16 +01:00
Daniel Leung
97db4ac0ca libc: add hidden option CONFIG_NEED_LIBC_MEM_PARTITION
The z_libc_partition was only enabled when newlib is being used,
and/or stack canaries are needed. This adds a hidden option
where this partition can be enabled if needed, regardless of
whether newlib is used or stack canaries are needed.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2020-10-24 10:52:00 -07:00
Nicolas Pitre
c227fe7b80 lib/os/heap: Correct aligned_alloc sizing for small heaps
The code that made aligned_alloc work with the 4-byte heap headers was
requesting a block of the correctly padded size, and correctly
aligning the output buffer within that memory, but it was using the
UNALIGNED chunk size for the buffer as the final size of the block
with splitting off the unused suffix.  So the final chunk in the
buffer was could be incorrectly returned to the heap and reused,
leading to overlap.

Compute the chunk size of the output buffer based on the
already-aligned output pointer instead.

Initial investigation and fix from Andy Ross <andrew.j.ross@intel.com>.
I reworked his fix, created a test case, and stolen his commit log.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-10-23 12:52:04 -04:00
Maksim Masalski
da3e834d58 lib: posix: unchecked return value while taking semaphore
Fixing of the false-positive Coveiry issue.
Stop Coverity from worrying about this case.

Signed-off-by: Maksim Masalski <maksim.masalski@intel.com>
2020-10-21 06:26:11 -05:00
Kumar Gala
cae988c697 lib: posix: eventfd: Fix use of 'struct k_spinlock_key'
Code should be using k_spinlock_key_t and not 'struct k_spinlock_key'.
With recent change to redefine struct k_spinlock_key we see this code
break because it wasn't using the correct type.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-10-07 08:11:25 -04:00
Arnaud Mouiche
7673baed84 libc: newlib: Avoid all files to be opened in line buffer mode
Default buffer mode is setup by fopen depending on isatty value.
Expect only 0, 1 & 2 to be tty for CONFIG_POSIX_API cases.

This way regular files are opened in block buffering mode (instead
of line buffering mode) which really speed up fread/fwrite
operations on FS.

Signed-off-by: Arnaud Mouiche <arnaud.mouiche@invoxia.com>
2020-10-05 09:59:38 -07:00
Arnaud Mouiche
fd9766c961 posix: fix missing _open for fopen support
Using fopen() in application failed to build when configured with
	CONFIG_NEWLIB_LIBC=y
	CONFIG_POSIX_API=y

Signed-off-by: Arnaud Mouiche <arnaud.mouiche@invoxia.com>
2020-10-05 09:59:38 -07:00
Xavier Chapron
824f423e54 misc: Replace assert include and calls by sys/__assert.h equivalent
Replace all calls to the assert macro that comes from libc by calls to
__ASSERT_NO_MSG(). This is usefull as the former might be different
depending on the libc used and the later can be customized to reduce
flash footprint.

Signed-off-by: Xavier Chapron <xavier.chapron@stimio.fr>
2020-10-02 11:42:40 +02:00
Rafał Kuźnia
5ead7e7b3b lib: posix: eventfd: general eventfd improvements
The eventfd implementation suffers from various shortcomings
and it is not thread safe.

This commit addresses the following aspects of eventfd:
* make read() and write() atomic in respect to each other
* POLLIN after creating eventfd with initval != 0 shall be set
* blocking and nonblocking modes shall have the same effect on poll()
* add support for POLLOUT

Signed-off-by: Rafał Kuźnia <rafal.kuznia@nordicsemi.no>
2020-10-02 11:34:19 +02:00
Christopher Friedt
3cdf718d5b lib: posix: nanosleep: round up to the nearest microsecond
We must round up to the nearest microsecond in order to fulfill the
nanosleep(2) API requirement of sleeping for *at least* that many
nanoseconds.

The only platform with an upper-bound check right now is Nordic.

Fixes #28483

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
2020-10-02 11:31:43 +02:00
Aastha Grover
83b9f69755 code-guideline: Fixing code violation 10.4 Rule
Both operands of an operator in the arithmetic conversions
performed shall have the same essential type category.

Changes are related to converting the integer constants to the
unsigned integer constants

Signed-off-by: Aastha Grover <aastha.grover@intel.com>
2020-10-01 17:13:29 -04:00
Gerard Marull-Paretas
faf7736b14 lib: gui: lvgl: fix touch point rotation equations
When rotation is 90 or 270 degrees X and Y resolution values will also
be swapped, so we need to take that into account.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2020-09-24 13:50:25 -05:00
Lauren Murphy
25f9b643c7 lib: os: mempool: Fix unused return values from sys_mutex_lock calls
Fixes Coverity issues 210682 and 210686.

Fixes #25983
Fixes #25984

Signed-off-by: Lauren Murphy <lauren.murphy@intel.com>
2020-09-18 18:02:37 -04:00
Kumar Gala
59f76a291c lib: cmsis_rtos_v2: Handle possible zero length array
Change how we handle the case of
CONFIG_CMSIS_V2_THREAD_DYNAMIC_MAX_COUNT=0 so that we don't create
a zero length array.  Instead we can just ifdef out the code associated
with handling a dynamic stack allocation.

Fixes #28397

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-09-18 10:13:58 -05:00
Gerard Marull-Paretas
e8459719d8 lib: gui: lvgl: match kscan rotation with the display
Kscan coordinates are transformed in case display is rotated so that
frames of reference are aligned.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2020-09-16 12:02:52 +02:00
Gerard Marull-Paretas
b28ec03f0e lib: gui: lvgl: add support for kscan axes swap and inversion
Add new Kconfig options to allow kscan axes swap and inversion. These
options are useful to align display and touch frame of reference. If a
touch API is ever introduced these options could be moved there.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2020-09-16 12:02:52 +02:00
Grzegorz Kostka
cccb1908ac lib: fdtable: fix z_free_fd multiple calls fd leak
Multiple calls of z_free_fd against fd with refcount equal 0 are causing
descriptor table entry leak by decrementing refcount below 0.
This patch prevents decrementing refcount below zero.

Signed-off-by: Grzegorz Kostka <grzegorz@mobility.cloud>
2020-09-10 16:04:36 -05:00
Peter Bigot
6c910c021e lib: os: printk: fix printing unsigned 32-bit integers
The vararg extraction for unmodified integers always used int, which
sign extends when assigned to the printk_val_t.  Avoid the sign
extension for unsigned values.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-09-10 11:46:56 -05:00
Peter Bigot
b3f0c3b4d7 lib: os: prf: correct use of istype functions
Character class functions from ctype.h may be implemented as macros
where the argument is used to index an array of class flags.  Using a
char value as an index produces diagnostics in some toolchains.
Explicitly cast the parameter to the type required by the API.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-09-03 21:53:09 +02:00
Peter Bigot
e812ee6c21 shell: support floating point output with newlib
shell_fprintf requires that formatted output be emitted with a
putchar()-like output function.  Newlib does not provide such a
capability.  Zephyr provides two solutions: z_prf() which is part of
minimal libc and handles floating point formatting, and z_vprintk()
which is core and does not support floating point.

Move z_prf() out of minimal libc into the core lib area, and use it
unconditionally in the shell.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-09-03 21:53:09 +02:00
Flavio Ceolin
03d70b2ca3 lib: hex: Remove constant expression
Part of an else expression was constant, just removing it.

Code guideline rule 14.3

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2020-09-02 13:45:50 -04:00
Flavio Ceolin
1396e89e6c os: work_q: Use NULL instead of 0
Code guideline rule 11.9 says to use macro NULL for null pointers.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2020-09-02 13:45:50 -04:00
Tomasz Bursztyka
e18fcbba5a device: Const-ify all device driver instance pointers
Now that device_api attribute is unmodified at runtime, as well as all
the other attributes, it is possible to switch all device driver
instance to be constant.

A coccinelle rule is used for this:

@r_const_dev_1
  disable optional_qualifier
@
@@
-struct device *
+const struct device *

@r_const_dev_2
 disable optional_qualifier
@
@@
-struct device * const
+const struct device *

Fixes #27399

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-09-02 13:48:13 +02:00
Vincent Wan
0e436ed72a lib: fdtable: fix reference counting in z_reserve_fd()
The new fd entry should be reserved by incrementing its reference count
in z_reserve_fd() instead of z_finalize_fd() in order to avoid having
the same one being returned in a concurrent call. If for some reason
the fd is not finalized after z_reserve_fd() is called, it can be
freed via z_free_fd(), which would decrement the reference count.

Fixes #27721

Signed-off-by: Vincent Wan <vwan@ti.com>
2020-08-27 11:42:19 +03:00
Gerard Marull-Paretas
7538f0212f lib: gui: lvgl: improve cmake formatting
Improve the format of the CMakeLists.txt file.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2020-08-26 15:32:59 -05:00
Gerard Marull-Paretas
b3847e80c4 lib: gui: lvgl: sort objects
LVGL objects have been alphabetically sorted. The aim of this change is
to help locating objects as well as keeping track of them.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2020-08-26 15:32:59 -05:00
Gerard Marull-Paretas
948a692252 lib: gui: lvgl: split Kconfig settings
LVGL Kconfig settings have been splitted into more granular units in
order to improve readability and maintenance.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2020-08-26 15:32:59 -05:00
Gerard Marull-Paretas
4f86a9a2d5 lib: gui: lvgl: align all Kconfig object names
Align all Kconfig option names with LVGL names. The followed rule:
LV_(.*) -> CONFIG_LVGL_(.*).

Also replaced LVGL boolean configuration entries using if/else/endif
with direct IS_ENABLED macro.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2020-08-26 15:32:59 -05:00
Flavio Ceolin
0aaae4a039 guideline: Make explicit fallthrough cases
-Wimplicit-fallthrough=2 requires a fallthrough comment or a compiler
to tells gcc that this happens intentionally.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2020-08-24 20:28:47 -04:00
Anas Nashif
b4cd1c1a53 lib: mempool: alloc_bit_is_set is used only with asserts
Put it behind an ifdef CONFIG_ASSERT to make clang happy.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2020-08-19 06:57:40 -04:00
Siddharth Chandrasekaran
0637595ec5 libc: add strtok_r implementation
This is a standard function and useful for applications.

Signed-off-by: Siddharth Chandrasekaran <siddharth@embedjournal.com>
2020-08-18 09:19:58 -07:00
Jukka Rissanen
2ed6b6a8ed lib: fdtable: Add callback for close() calls
Do not route close() calls via ioctl() as that is error prone
and quite pointless. Instead create a callback for close() in
fdtable and use it directly.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2020-08-14 09:47:51 -07:00
Peter A. Bigot
13f96d439e sys: onoff: support clients using synchronous transitions
The on-off manager infrastructure is designed to robust asynchronous
transition between binary states where multiple clients may be
initiating a transition from any context.  The actual transition is
performed using a manager that tracks the current state and pending
operations.  Requests are initiated by passing a reference to an
onoff_client object that holds client state including the notification
mechanism.

This API may be used in subsystems where the transitions for a
particular driver are always synchronous and isr-ok, e.g. setting a
SoC-controlled GPIO.  In this situation the full on-off manager
infrastructure is wasteful.  All we need is a record of the service
state: off, active count, or error.

Add a data structure and an API that can be used to replace the onoff
manager functionality in a situation where all transitions are isr-ok
and synchronous while retaining compatible behavior from the client
perspective.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2020-08-14 17:53:39 +02:00
Yuguo Zou
1672e51d51 lib: fix compile error for ARC arch
macro Z_ARC_MPU_SIZE_ALIGN is no longer exist, so change it accordingly

Signed-off-by: Yuguo Zou <yuguo.zou@synopsys.com>
2020-08-12 09:22:37 +02:00
Jan Pohanka
9e462469b4 lib: posix: Fix priority validity check in pthread_setschedparam
New priority value check was faultly done on zephyr priority instead
of posix one.

Signed-off-by: Jan Pohanka <xhpohanka@gmail.com>
2020-08-11 07:46:06 -04:00
Jukka Rissanen
8b578bdceb lib: os: Add refcounting to fdtable entries
Use proper refcounting instead of magic value in obj field
when checking whether the fd is still in use. This will make
sure that if fd is shared between two threads, we do not
release it too soon.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2020-08-10 14:56:08 -07:00
Torsten Rasmussen
bb3946666f cmake: fix include directories to work with out-of-tree arch
Include directories for ${ARCH} is not specified correctly.
Several places in Zephyr, the include directories are specified as:
${ZEPHYR_BASE}/arch/${ARCH}/include
the correct line is:
${ARCH_DIR}/${ARCH}/include
to correctly support out of tree archs.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2020-08-05 08:06:07 -04:00
Gerson Fernando Budke
29544a1ceb updatehub: Move from lib to subsys:mgmt folder
Zephyr introduced subsys/mgmt folder for MCU management. Move UpdateHub
to this newly and dedicated space.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-08-03 16:33:06 +02:00
Carles Cufi
244f826e3c cmake: remove _if_kconfig() functions
This set of functions seem to be there just because of historical
reasons, stemming from Kbuild. They are non-obvious and prone to errors,
so remove them in favor of the `_ifdef()` ones with an explicit
`CONFIG_` condition.

Script used:

git grep -l _if_kconfig | xargs sed -E -i
"s/_if_kconfig\(\s*(\w*)/_ifdef(CONFIG_\U\1\E \1/g"

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2020-08-01 12:35:20 +02:00
Dominik Ermel
49ab2099f3 lib/posix: Add support for open flags to open(...)
The fs_open flags has been changed to accept open flags, which requires
changes to open(...) to support the new flags.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-07-30 16:33:18 +02:00
Dominik Ermel
deb1694c3c posix: Extend open() function signature with ellipsis
The commit changes signature of open function from:
  int open(const char *name, int flags)
to
  int open(const char *name, int flags, ...)

Currently existing two argument invocations should not require any
rework.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-07-30 16:33:18 +02:00
Dominik Ermel
47ab70e34c gui: Add open flags argument to fs_open invocation
Now fs_open supports open flags and the lvgl_fs code requires update.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-07-30 16:33:18 +02:00
Gerson Fernando Budke
897a177232 lib: updatehub: Fix getaddrinfo resource leak
Add missing freeaddrinfo to fix memory leak.

Fixes #26994.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-07-30 10:29:21 -04:00
Gerson Fernando Budke
32bafacc31 lib: updatehub: Add flash_img_init return value check
The flash_img_int return value is not checked for fail conditions.
This can result on useless download attempts once image will not
be properly recorded. Add return value check and on error execute
default treatment.

Fixes #26992.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-07-30 12:24:48 +02:00
Gerson Fernando Budke
8683b47b17 lib: updatehub: Fix out-of-bounds access
The struct pollfd context variable is not proper initialized and index
is out-of-bounds. Adjusts index to be inside scope boundary.

Fixes #26993.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-07-30 12:24:48 +02:00
Gerard Marull-Paretas
5df0ad3194 lib: gui: lvgl: update to v7.0.2
Update LVGL to version 7.0.2. Notable changes in v7 include:

- New drawing system (note that it uses much more ROM than previous
  versions)
- New style system
- Some objects have been renamed (current changes do not align yet
  Zephyr Kconfig settings with LVGL)
- New fonts with more sizes (e.g. Montserrat, replacing Roboto)
- Theme changes (most have been removed, default is now Material)

Note that constant defaults have been aligned with LVGL.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2020-07-27 15:06:22 +02:00
Vincent Wan
b3d5af0e9d lib: fdtable: Remove workaround for SimpleLink for fcntl
Given socket offloading is now implemented under the fd's vtable, we can
directly use the default fcntl implementation.

Signed-off-by: Vincent Wan <vwan@ti.com>
2020-07-27 13:28:06 +02:00
Daniel Leung
4fc1444823 lib: os/heap: timeout: avoid identifier collisions
MISRA-C Rule 5.3 states that identifiers in inner scope should
not hide identifiers in outer scope.

In the function sys_heap_alloc(), the variable "chunksz"
collide with function named chunksz(). So rename those variable.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2020-07-25 21:26:15 -04:00
David Leach
2abdc19ceb libc: newlib: libc-hooks: Fix Coverity warning
Suppress the coverity warning on using the semaphore as
this semaphore is used and freed only in this function.

Fixes: #18960

Signed-off-by: David Leach <david.leach@nxp.com>
2020-07-24 21:51:14 -04:00
Pete Skeggs
3835b0bd1c lib/os/work_q: sanity check work_q handler prior to calling it
Just as NULL pointers should not be dereferenced, they should
not be called either.

Fixes 26723

Signed-off-by: Pete Skeggs <peter.skeggs@nordicsemi.no>
2020-07-24 12:07:17 +02:00
Andrew Boie
bb0c6dbf62 libc: newlib: remove impossible ifdef
This whole code block is ifdef'ed around
CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE being NOT defined,
remove as this can never be true.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2020-07-23 11:01:42 +02:00
Andrew Boie
ca1154d85a libc: newlib: simplfy malloc arena defn
We've come a long way since this was written to implement
generic ram bounds definitions and MPU capabilities,
use them here.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2020-07-23 11:01:42 +02:00
Jan Van Winkel
1130dc9f90 gui: Set actual display size when using static buffers
Set actual display size, obtained via the display driver, when using
static rendering buffers. If the actual screen size is not set an
out-of-bound write could occur in case the maximum resolution settings
for LVGL are larger than the actual screen resolution.

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
2020-07-21 15:12:05 +02:00
Marcin Niestroj
063dd51dc7 lib: posix: eventfd: fix read/poll after multiple writes
So far semaphore was used with possible values in range 0 to
UINT32_MAX. Each write resulted in semaphore increment. As an example
after two writes and single read eventfd counter was correctly zeroed,
but semaphore counter was not. This means that poll() signalled at this
stage POLLIN (semaphore counter was > 0), but it clearly should
not (eventfd counter == 0). Blocking version of read() was also
returning immediately, returning 0 as previous eventfd counter.

Change read_sem to be a binary semaphore, which counter represents
eventfd counter being zero (when semaphore counter == 0) or
non-zero (when semaphore counter == 1). Try to take the semaphore in
eventfd read() and decrement eventfd counter when semaphore was ready.

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
2020-07-20 16:21:37 +02:00
Christopher Friedt
d0629e3c4e libc: malloc: log an error when malloc is called and arena size is 0
Previously, if the arena size was zero, malloc would always fail.
However, the log message was only visible if debug messages were
enabled. Logging an error will hopefully make it more obvious that
CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE should be >= if the minimal
libc and malloc are both used.

Fixes #26720

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
2020-07-20 12:48:33 +02:00
Nicolas Pitre
6014e5f441 lib/os/heap: remove big_heap restriction for aligned allocations
After commit 8a6b02b5bf ("lib/os/heap: some code simplification in
sys_heap_aligned_alloc()") it is no longer required to have a "big"
heap for aligned allocations to work on 32-bit targets. While the
natural alignment for returned memory has an offset of 4 within a chunk
unit due to the smaller header size, returning to a chunkid from a
memory pointer with an offset of 8 will fall back onto the proper chunk
number once the 4 is substracted and then divided by 8.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-07-14 19:35:52 -04:00
Nicolas Pitre
e9ff53fa2a lib/os/heap: optimize chunk splitting
The code is doing a split in split_alloc(), adding the leftover to the
free list, then splitting the suffix away in sys_heap_aligned_alloc(),
removing the former leftover from the free list, combining it with the
suffix and finally adding the combined chunk back to the free list.

Instead, let's have each allocator do their own splitting only once by
moving the split_alloc() processing upstream rather than downstream.
This also allows for the "used" flag to be set only once at the end
rather than being overwritten along the way.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-07-14 19:35:52 -04:00
Nicolas Pitre
9b538e4079 lib/os/heap: make "solo free headers" into first-class citizens
Instead of limiting the excess split-off to sufficiently large chunks
in split_alloc(), let's allow normal allocations to create "solo free
headers" just like with aligned allocations. There is no point leaving
them in the allocated chunk if the user didn't ask for it. Doing so
makes them eligible for merging at the next opportunity and potentially
reusable sooner.

Also make the validation code aware of them.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-07-14 19:35:52 -04:00
Nicolas Pitre
130963ad2f lib/os/heap: add an additional validation criteria
One fundamental validation criteria is to never have consecutive free
chunks. If that ever happens we failed to merge them. That means a free
chunk must always be surrounded by used chunks.

It is a pain to extend valid_chunk() with new rules as it is.
So a VALIDATE() macro is introduced to make things easier to work with.
It also allows for isolating each test, possibly making VALIDATE() into
__ASSERT() to determine exactly which test is tripping when debugging.

Finally, because of that new validation rule, sys_heap_validate() must
be modified so not to use valid_chunk() while it is flipping all the
"used" flags. So let's run valid_chunk() up front before alterating
chunk headers.

Now sys_heap_validate() has become justifiably more expensive and a few
emulated targets are about to bust the tests/lib/heap test timeout. So
bump the timeout as well.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-07-14 19:35:52 -04:00
Nicolas Pitre
9b617755d2 lib/os/heap: code cleanup
This makes the code cleaner wrt bucket_idx() usage on chunks for which
solo_free_header() is true. In such case the bucket_idx() computation
is useless, and potentially undefined anyway.

In the same vain, move the clearing of the used flag out of
free_chunks() as only one of its callers actually needs that.
Makes free_chunks singular as there is only one chunk (potentially
spanning multiple chunk units) to free.

Also some cosmetic changes for better code uniformity.

No functional changes.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-07-14 19:35:52 -04:00
Dominik Ermel
5a40ff1ac5 posix/fs: Fix POSIX lseek to return position upon completion
Bring standard behaviour to lseek, where new file position is returned
on success.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-07-04 07:46:51 -04:00
Andy Ross
3f9ad86b1d kernel/printk: Make it synchronous
Currently printk isn't synchronized except at the byte output level,
leading to interleaving of messages on SMP systems that try to log
simultaneously.  This is actually fairly amusing, and actually helpful
occasionally to validate inter-CPU contention down to the "few cycles"
level.

Still, when you're printing data you need to read, you need to be able
to read it.  Put a spinlock around each buffered line.  This has to
happen in a few places, as there are three different code paths taken
for !USERSPACE, syscall, and user mode.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-06-27 08:14:58 -04:00
Nicolas Pitre
ae597c07b6 printk: print %p properly on 32-bit targets
The width for %p on 32-bit targets should be 8 regardless of
CONFIG_PRINTK64. Adjust the test accordingly.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-06-27 00:03:58 -04:00
Nicolas Pitre
64af35049c lib/os/heap: debugging facility to dump the heap structure to the cconsole
It is linked in only when used, so handy to always have it around for
analysis purposes.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-06-26 11:41:43 -07:00
Nicolas Pitre
0ae04f01b6 lib/os/heap: make some checks more assertive
Some checks in sys_heap_init() depend on the externally provided size
parameter. If the check fails, this would be a bug outside of the heap
code and therefore should be flagged despite the value of
CONFIG_SYS_HEAP_VALIDATE.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-06-25 17:43:13 -07:00
Andy Ross
973487fdad lib/os: Rework/shrink printk conversions, add 64 bit support
Add support for 64 bit conversions in a uniformly expressable way by
printing values backwards into a buffer on the stack first.  This
allows all operations to work on the low bits of the value and so the
code doesn't need to care (beyond the size of that buffer) about the
word size.  This trick also doesn't care about the specifics of the
base value, so in the process this unifies the decimal and hex printk
conversion code to a single function.

This comes at a mild cost in CPU cycles to the decimal converter and
somewhat higher cost to hex (because it's now doing a full div/mod
operation instead of shifting and masking).  And stack usage has grown
by a few words to hold the temporary.  But the benefits in code size
are substantial (e.g. ~250 bytes of .text on arm32).

Note that this also contains a change to tests/kernel/common to
address what appears to have been a bug in the original converters.
The printk test uses a format string that looks like "%-4x%-2p" and
feeds it the literal arguments "0xABCDEF" and "(char *)42".
Now... clearly both those results are going to overflow the 4 and
2-byte field sizes, so there shouldn't be any whitespace between these
fields.  But the test was written to expect two spaces, inexplicably
(yes, I checked: POSIX-compatible printf implementations don't have
those spaces either).

The new code is definitely doing the right thing, so fix the test
instead.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-06-24 13:43:40 -07:00
Nicolas Pitre
8a6b02b5bf lib/os/heap: some code simplification in sys_heap_aligned_alloc()
It is clearer to apply the alignment in the memory address space
rather than the chunk space.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-06-24 11:53:50 -07:00
Gerson Fernando Budke
3add3d7b60 lib: updatehub: Add download block check
The current CoAP implementation not perform any checks including
duplicated packets. This add block sequency verification and a
timer to ensures that slow networks works apropriately.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-06-23 19:22:31 +02:00
Gerson Fernando Budke
092b962350 lib: updatehub: Kconfig: Add coap block size option
Allow select max CoAP block size for exchange data using coap protocol.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-06-23 19:22:31 +02:00
Gerson Fernando Budke
6c4c12d531 lib: updatehub: Kconfig: Add coap max retry option
The current implementation uses a fixed value for max retries. That
value could be good for an wired network like Ethernet. However,
wireless network can suffer with higher packet collision, low reception
signal etc. This refacts the variable to be defined at Kconfig. This
way max retries can be adjust conform the current media.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-06-23 19:22:31 +02:00
Gerson Fernando Budke
a7c63d8818 lib: updatehub: Init metadata variable
Init metadata variable to a defined state.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-06-23 19:22:31 +02:00
Gerson Fernando Budke
d16517e575 lib: updatehub: Init hints variable
The hints variable is used without a defined state. This fill the struct
with zeros to set variable at a well known state.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-06-23 19:22:31 +02:00
Gerson Fernando Budke
9a43250667 lib: updatehub: Add log level
Current log only prints default log level. Add LOG_LEVEL at updatehub
to switch between log variations based on CONFIG_UPDATEHUB_LOG_LEVEL.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-06-23 19:22:31 +02:00
Andy Ross
ed258e9c6f lib/os/heap: Add sys_heap_aligned_alloc()
Add support for a C11-style aligned_alloc() in the heap
implementation.  This is properly optimized, in the sense that unused
prefix/suffix data around the chosen allocation is returned to the
heap and made available for general allocation.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-06-22 14:54:04 -04:00
Andy Ross
1f29dd3251 lib/os/heap: General refactoring
Miscellaneous refactoring and simplification.  No behavioral changes:

Make split_alloc() take and return chunk IDs and not memory pointers,
leaving the conversion between memory/chunks the job of the higher
level sys_heap_alloc() API.  This cleans up the internals for code
that wants to do allocation but has its own ideas about what to do
with the resulting chunks.

Add split_chunks() and merge_chunks() utilities to own the linear/size
pointers and have split_alloc() and free_chunks() use them instead of
doing the list management directly.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-06-22 14:54:04 -04:00
Nicolas Pitre
ad59e923e9 sys_heap: reduce the size of struct z_heap_bucket by half
This struct is taking up most of the heap's constant footprint overhead.
We can easily get rid of the list_size member as it is mostly used to
determine if the list is empty, and that can be determined through
other means.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-06-21 19:25:35 +02:00
Nicolas Pitre
74fbca412a sys_heap: perform cheap overflow detection on freed memory
Make the LEFT_SIZE field first and SIZE_AND_USED field last (for an
allocated chunk) so they sit right next to the allocated memory. The
current chunk's SIZE_AND_USED field points to the next (right) chunk,
and from there the LEFT_SIZE field should point back to the current
chunk. Many trivial memory overflows should trip that test.

One way to make this test more robust could involve xor'ing the values
within respective accessor pairs. But at least the fact that the size
value is shifted by one bit already prevent fooling the test with a
same-byte corruption.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-06-21 19:25:35 +02:00
Nicolas Pitre
cb3d460a2c sys_heap: simplify some complex checks
Avoid redundancy and bucket_idx() usage when possible.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-06-21 19:25:35 +02:00
Nicolas Pitre
d1125d21d4 sys_heap: remove need for last_chunk()
We already have chunk #0 containing our struct z_heap and marked as
used. We can add a partial chunk at the very end that is also marked
as used. By doing so there is no longer a need for checking heap
boundaries at run time when merging/splitting chunks meaning fewer
conditionals in the code's hot path.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-06-21 19:25:35 +02:00
Nicolas Pitre
6d827fa080 sys_heap: introduce min_chunk_size()
With this we can remove magic constants, especially those used with
big_heap().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-06-21 19:25:35 +02:00
Nicolas Pitre
e553161b8e sys_heap: optimize struct z_heap
It is possible to remove a few fields from struct z_heap, removing
some runtime indirections by doing so:

- The buf pointer is actually the same as the struct z_heap pointer
  itself. So let's simply create chunk_buf() that perform a type
  conversion. That type is also chunk_unit_t now rather than u64_t so
  it can be defined based on CHUNK_UNIT.

- Replace the struct z_heap_bucket pointer by a zero-sized array at the
  end of struct z_heap.

- Make chunk #0 into an actual chunk with its own header. This allows
  for removing the chunk0 field and streamlining the code. This way
  h->chunk0 becomes right_chunk(h, 0). This sets the table for further
  simplifications to come.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-06-21 19:25:35 +02:00
Nicolas Pitre
e2b64777e5 sys_heap: optimize usage of size and used flags
By storing the used flag in the LSB, it is no longer necessary to have
a size_mask variable to locate that flag. This produces smaller and
faster code.

Replace the validation check in chunk_set() to base it on the storage
type.

Also clarify the semantics of set_chunk_size() which allows for clearing
the used flag bit unconditionally which simplifies the code further.

The idea of moving the used flag bit into the LEFT_SIZE field was
raised. It turns out that this isn't as beneficial as it may seem
because the used bit is set only once i.e. when the memory is handed off
to a user and the size field becomes frozen at that point. Modifications
on the leftward chunk may still occur and extra instructions to preserve
that bit would be necessary if it were moved there.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-06-21 19:25:35 +02:00
Nicolas Pitre
54950aca01 sys_heap: provide more chunk_fields accessors
Let's provide accessors for getting and setting every field to make the
chunk header layout abstracted away from the main code. Those are:

SIZE_AND_USED: chunk_used(), chunk_size(), set_chunk_used() and
chunk_size().

LEFT_SIZE: left_chunk() and set_left_chunk_size().

FREE_PREV: prev_free_chunk() and set_prev_free_chunk().

FREE_NEXT: next_free_chunk() and set_next_free_chunk().

To be consistent, the former chunk_set_used() is now set_chunk_used().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-06-21 19:25:35 +02:00
Nicolas Pitre
f97eca26e6 sys_heap: some cleanups to make the code clearer
First, some renames to make accessors more explicit:

  size() --> chunk_size()
  used() --> chunk_used()
  free_prev() --> prev_free_chunk()
  free_next() --> next_free_chunk()

Then, the return type of chunk_size() is changed from chunkid_t to
size_t, and chunk_used() from chunkid_t to bool.

The left_size() accessor is used only once and can be easily substituted
by left_chunk(), so it is removed.

And in free_list_add() the variable b is renamed to bi so to be
consistent with usage in sys_heap_alloc().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2020-06-21 19:25:35 +02:00
Markus Fuchs
2f9b0d419b json: Add top-level array encoding support
The library supports the declaration of JSON arrays as both nested and
top-level elements. However, as the provided encoding functions
json_obj_encode() and json_obj_encode_buf() interpret all input
structures as objects, top-level arrays are encoded as

{"<field_name>":[{...},...,{...}]}

instead of

[{...},...,{...}].

Add new functions json_arr_encode() and json_arr_encode_buf() that
enable top-level JSON array encoding.

Signed-off-by: Markus Fuchs <markus.fuchs@de.sauter-bc.com>
2020-06-19 18:21:27 +02:00
Paul Sokolovsky
5f05d6598f libc: newlib: libc-hooks: Provide our own implementation of __chk_fail()
The version as shipped in Newlib itself is coded a bit sloppily for an
embedded environment. We thus want to override it (and make it weak, to
allow user apps to override it in turn, if needed). The desired
properties of the implementation are:

1. It should call _write() (Newlib implementation calls write()).
2. It should be minimal (Newlib implementation allocates message
on the stack, i.e. misses "static const").

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2020-06-17 17:06:47 +02:00
Marcin Niestroj
385abc7740 lib: posix: eventfd: support build with !POSIX_API
Just change include path to support build with CONFIG_POSIX_API=n.

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
2020-06-15 16:52:29 +02:00
Christopher Friedt
8bcf005084 lib: posix: nanosleep
This change adds support for nanosleep(2) documented at
https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html

N.B: Currently, this provides no better resolution than
k_busy_wait()

Fixes #25554

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
2020-06-12 17:02:03 +02:00
Marcin Niestroj
1502b9a648 lib: posix: eventfd: move intialization out of search loop
Search for unused eventfd object and just remember its instance in loop
body. Initialize object later, to make it distinct from "search
phase". This change is basically an improvement for readability.

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
2020-06-10 17:19:19 +03:00
Kumar Gala
a1b77fd589 zephyr: replace zephyr integer types with C99 types
git grep -l 'u\(8\|16\|32\|64\)_t' | \
		xargs sed -i "s/u\(8\|16\|32\|64\)_t/uint\1_t/g"
	git grep -l 's\(8\|16\|32\|64\)_t' | \
		xargs sed -i "s/s\(8\|16\|32\|64\)_t/int\1_t/g"

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-06-08 08:23:57 -05:00
Andrew Boie
87480cd4fb fdtable: init fd context objects
Anytime a file descriptor context object is updated, we need to
reset its access permissions and initialization state. This
is the most centralized place to do it.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2020-06-03 22:33:32 +02:00
Tahir Akram
ba88c99677 lib: updatehub: Fix bus fault
Fix variable-size string copy patch that introduced a runtime bug that
causes a bus fault.

Fixes #24853.

Signed-off-by: Tahir Akram <mtahirbutt@hotmail.com>
Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-05-26 14:37:54 +02:00
Gerson Fernando Budke
284e841bf4 lib: updatehub: Fix compile warning
Based on the current platform a warning can raise becase of missing
string.h include file.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-05-18 11:12:31 +02:00
Gerson Fernando Budke
c35cad52f5 lib: updatehub: Add missing include for FLASH_AREA
The conversion from DT_FLASH_AREA to FLASH_AREA macros don't add the
storage flash_map.h include file.

Fixes: #25332

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-05-18 11:12:31 +02:00
Kumar Gala
ff579a3af3 flash: Convert DT_FLASH_AREA to FLASH_AREA macros
Convert with a combo of scripts and by hand fixups:

git grep -l DT_FLASH_AREA_.*_ID | \
 xargs sed -i -r 's/DT_FLASH_AREA_(.*)_ID/FLASH_AREA_ID(\L\1)/'

git grep -l DT_FLASH_AREA_.*_OFFSET | \
 xargs sed -i -r 's/DT_FLASH_AREA_(.*)_OFFSET/FLASH_AREA_OFFSET(\L\1)/'

git grep -l DT_FLASH_AREA_.*_SIZE | \
 xargs sed -i -r 's/DT_FLASH_AREA_(.*)_SIZE/FLASH_AREA_SIZE(\L\1)/'

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-05-13 21:22:53 +02:00
Gerson Fernando Budke
4980a13fc4 lib: updatehub: Update to new timeout api
Update to new timeout api. Without this change UpdateHub don't build
anymore.

Fixes: #25230

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-05-12 20:17:38 +02:00
Paul Sokolovsky
0b634793cc lib: posix: Update to new timeout API
Mostly trivial search-and-replace, except for pthread_rwlock.c, where
we need spread timeout over 2 semaphore operations.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2020-05-08 17:31:45 +02:00
Andy Ross
7e3d43b558 lib/cmsis_rtos_v2: Update to newer timeout API
Mostly simple.  Note that the CMSIS RTOS2 API specifies timeout values
in system ticks instead of milliseconds, so the conversions here are
able to elide a conversion that the original code had to do.  That's a
good thing, but does mean that in practice runtime behavior will not
be 1:1 identical.

Also note that the switch away from legacy timeouts involved a change
to 64 bit timeouts by default, which pushed
tests/portability/cmsis_rtos_v2 over the limit on qemu_xtensa.
Unfortunately CMSIS stacks have a fixed limit we can't increase, so I
turned off 64 bit timeouts (CMSIS apps won't need them by definition
anyway -- their API is 32 bit).

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-05-06 06:05:03 -04:00
Andy Ross
1003ab806c subsys/cmsis_v1: Port to new timeout API
No complexity here.  The CMSIS API was always in milliseconds, needs
nothing but a few wrapper macros for kernel timeout arguments.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-05-06 06:05:03 -04:00
David Brown
69e8a7b657 updatehub: Require peer verification with DTLS
DTLS without peer verification offers no security whatsoever (and is
arguably worse than not using DTLS in the first place).

Change the verification option to require this peer verification.  To
use this, it may be necessary to install and use a root certificate.

Signed-off-by: David Brown <david.brown@linaro.org>
2020-05-05 13:40:12 -05:00
Kumar Gala
be97e40683 soc: nios2: Cleanup linker scripts to use new DTS macros
Move defines for _RAM_ADDR, _RAM_SIZE, _ROM_ADDR, and _ROM_ADDR into
the linker.ld and thus remove dts_fixup.h.  We rework to use
DT_REG_ADDR and DT_REG_SIZE on DT_CHOSEN(zephyr_sram) and
DT_CHOSEN(zephyr_flash).

Also fixup use of _RAM_ADDR/_RAM_SIZE in newlib/libc-hooks.c.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-04-30 20:59:13 -05:00
Kumar Gala
a45ea3806f x86: Rework rework x86 related code to use new DTS macros
Replace DT_PHYS_RAM_ADDR and DT_RAM_SIZE with DT_REG_ADDR/DT_REG_SIZE
for the DT_CHOSEN(zephyr_sram) node.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-04-30 08:37:18 -05:00
Peter Bigot
a09f6ad54c json: fix buffer overrun in encoding helper
The bounds check failed to account for the additional space required
for the terminating NUL after the encoded value was written.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-04-28 15:06:45 -04:00
Tobias Svehagen
ca872a44c1 lib: posix: Add support for eventfd
This implements a file descriptor used for event notification that
behaves like the eventfd in Linux.

The eventfd supports nonblocking operation by setting the EFD_NONBLOCK
flag and semaphore operation by settings the EFD_SEMAPHORE flag.

The major use case for this is when using poll() and the sockets that
you poll are dynamic. When a new socket needs to be added to the poll,
there must be some way to wake the thread and update the pollfds before
calling poll again. One way to solve it is to have a timeout set in the
poll call and only update the pollfds during a timeout but that is not
a very nice solution. By instead including an eventfd in the pollfds,
it is possible to wake the polling thread by simply writing to the
eventfd.

Signed-off-by: Tobias Svehagen <tobias.svehagen@gmail.com>
2020-04-28 09:57:41 +03:00
Peter Bigot
14e2ca4f16 sys: onoff: redesign to meet changed needs
The previous architecture proved unable to support user expectations,
so the API has been rebuilt from first principles.  Backward
compatibility cannot be maintained for this change.

Key changes include:

* Formerly the service-provided transition functions were allowed to
  sleep, and the manager took care to not invoke them from ISR
  context, instead returning an error if unable to initiate a
  transition.  In the new architecture transition functions are
  required to work regardless of calling context: it is the service's
  responsibility to guarantee the transition will proceed even if it
  needs to be transferred to a thread.  This eliminates state machine
  complexities related to calling context.
* Constants identifying the visible state of the manager are exposed
  to clients through both notification callbacks and a new monitor API
  that allows clients to be notified of all state changes.
* Formerly the release operation was async, and would be delayed for the
  last release to ensure a client would exist to be notified of any
  failures.  It is now synchronous.
* Formerly the cancel operation would fail on the last client associated
  with a transition.  The cancel operation is now synchronous.
* A helper function is provided to safely synchronously release a
  request regardless of whether it has completed or is in progress,
  satisfying the use case underlying #22974.
* The user-data parameter to asynchronous notification callbacks has
  been removed as user data can be retrieved from the CONTAINER_OF
  the client data.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-04-22 16:52:47 +02:00
Julien D'Ascenzio
e689277dd5 lib: posix: clock: Turn clock_gettime into system call
Fix thread fault, on user mode, when reading variable rt_clock_base.
For the moment, clock_settime is left without system call:
we don't want to expose clock_settime without figuring out access
control

Signed-off-by: Julien D'Ascenzio <julien.dascenzio@paratronic.fr>
2020-04-17 08:08:37 -04:00
Gerson Fernando Budke
894dcbbf15 lib: updatehub: Improve probe security
Improve buffer overflow security on probe_cb. This ensures that socket
buffer have fixed lenght and content received by COAP fills properly on
metadata buffer. After that, ensures that metadata content is a valid
string with length lower than metadata size.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-04-17 10:09:56 +03:00
Gerson Fernando Budke
6238e52062 lib: updatehub: Refact to use bin2hex
Use bin2hex instead inline conversion.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-04-17 10:09:56 +03:00
Gerson Fernando Budke
0801aef3fb lib: updatehub: Fix variable-size string copy
A malformed JSON payload that is received from an UpdateHub server
may trigger memory corruption in the Zephyr OS. This could result
in a denial of service in the best case, or code execution in the
worst case.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
2020-04-17 10:09:56 +03:00
Andy Ross
aa4227754c lib/os: Add sys_heap, a new/simpler/faster memory allocator
The existing mem_pool implementation has been an endless source of
frustration.  It's had alignment bugs, it's had racy behavior.  It's
never been particularly fast.  It's outrageously complicated to
configure statically.  And while its fragmentation resistance and
overhead on small blocks is good, it's space efficiencey has always
been very poor due to the four-way buddy scheme.

This patch introduces sys_heap.  It's a more or less conventional
segregated fit allocator with power-of-two buckets.  It doesn't expose
its level structure to the user at all, simply taking an arbitrarily
aligned pointer to memory.  It stores all metadata inside the heap
region.  It allocates and frees by simple pointer and not block ID.
Static initialization is trivial, and runtime initialization is only a
few cycles to format and add one block to a list header.

It has excellent space efficiency.  Chunks can be split arbitrarily in
8 byte units.  Overhead is only four bytes per allocated chunk (eight
bytes for heaps >256kb or on 64 bit systems), plus a log2-sized array
of 2-word bucket headers.  No coarse alignment restrictions on blocks,
they can be split and merged (in units of 8 bytes) arbitrarily.

It has good fragmentation resistance.  Freed blocks are always
immediately merged with adjacent free blocks.  Allocations are
attempted from a sample of the smallest bucket that might fit, falling
back rapidly to the smallest block guaranteed to fit.  Split memory
remaining in the chunk is always returned immediately to the heap for
other allocation.

It has excellent performance with firmly bounded runtime.  All
operations are constant time (though there is a search of the smallest
bucket that has a compile-time-configurable upper bound, setting this
to extreme values results in an effectively linear search of the
list), objectively fast (about a hundred instructions) and amenable to
locked operation.  No more need for fragile lock relaxation trickery.

It also contains an extensive validation and stress test framework,
something that was sorely lacking in the previous implementation.

Note that sys_heap is not a compatible API with sys_mem_pool and
k_mem_pool.  Partial wrappers for those (now-) legacy APIs will appear
later and a deprecation strategy needs to be chosen.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-04-14 10:05:55 -07:00
Peter Bigot
8bd676ed38 sys: onoff: generalize and shorten API
The original API was misnamed, as the intent was to provide a manager
that decoupled state management from the service that needed to be
turned on or off.  Update all the names, shortening them where
appropriate removing unncessary internal components like _service.

Also remove some API that misled developers into believing that onoff
managers are normally expected to be exposed directly to consumers.
While this is a use case, in most situations there are service or
client-specific actions that need to be coupled to transition events.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-04-06 16:41:41 +02:00
Peter Bigot
fadd98aad2 sys: add generic asynchronous notification infrastructure
k_poll() for a signal is often desired for notification of completion
of asynchronous operations, but there are APIs where it may be
necessary to invoke "asynchronous" operations from contexts where
sleep is disallowed, or before the kernel has been initialized.
Extract the general notification solution from the on-off service into
a utility that can be used for other APIs.

Also move documentation out to a resource management section.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-04-06 16:41:41 +02:00
Krzysztof Chruscinski
e2ca46c329 sys: onoff: Move transition functions out of service struct
Extracted transition functions from onoff structure to external one
which allows to keep them in flash.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2020-04-06 16:41:41 +02:00
Joakim Andersson
4ebfafe7ce lib: os: fix signed and unsigend comparison warnings
Fix instances of:
warning: comparison between signed and unsigned integer expressions
[-Wsign-compare]

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-04-03 18:06:59 -04:00
Arnaud Pouliquen
25ec73986b lib: open-amp: add helper to add resource table in project
The resource table is needed by the Linux kernel OS
for a rpmsg generic support, but is also recognised by OpenAMP.
This table allows to add trace based on the RAM console
and to support rpmsg protocol.

Signed-off-by: Arnaud Pouliquen <arnaud.pouliquen@st.com>
2020-04-01 09:21:15 -05:00
Andy Ross
7832738ae9 kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument.  Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created.  This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.

The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.

The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.

Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.

For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided.  When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.

Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions.  These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig.  These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.

k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.

Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate.  Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure.  But k_poll() does not fail
spuriously, so the loop was removed.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-31 19:40:47 -04:00
Andy Ross
32bb2395c2 timeout: Fix up API usage
Kernel timeouts have always been a 32 bit integer despite the
existence of generation macros, and existing code has been
inconsistent about using them.  Upcoming commits are going to make the
timeout arguments opaque, so fix things up to be rigorously correct.
Changes include:

+ Adding a K_TIMEOUT_EQ() macro for code that needs to compare timeout
  values for equality (e.g. with K_FOREVER or K_NO_WAIT).

+ Adding a k_msleep() synonym for k_sleep() which can continue to take
  integral arguments as k_sleep() moves away to timeout arguments.

+ Pervasively using the K_MSEC(), K_SECONDS(), et. al. macros to
  generate timeout arguments.

+ Removing the usage of K_NO_WAIT as the final argument to
  K_THREAD_DEFINE().  This is just a count of milliseconds and we need
  to use a zero.

This patch include no logic changes and should not affect generated
code at all.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-31 19:40:47 -04:00
Oleg Zhurakivskyy
b1e1f64d14 global: Replace BUILD_ASSERT_MSG() with BUILD_ASSERT()
Replace all occurences of BUILD_ASSERT_MSG() with BUILD_ASSERT()
as a result of merging BUILD_ASSERT() and BUILD_ASSERT_MSG().

Signed-off-by: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
2020-03-31 07:18:06 +02:00
Gerson Fernando Budke
d1e2d345fb lib: updatehub: Add missing do upgrade request call
After a success image download, UpdateHub needs inform MCUboot that
must test new image and then, on success, commit this new image. This
add missing upgrade request call step and fixes the upgarde flow.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
2020-03-30 16:54:29 +03:00
Gerson Fernando Budke
1128eab3f2 lib: updatehub: Fix download block error
The current version aborts update when found last transfer block. Now,
system checks only at end of coap block transfer total size and install
if download is ok.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
2020-03-30 16:54:29 +03:00
Gerson Fernando Budke
1fe1b0eec6 lib: updatehub: Extract sha256 final method
Extract finish sha256 calc method.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
2020-03-30 16:54:29 +03:00
Gerson Fernando Budke
5f5919a900 lib: updatehub: Fix buffer sizes
The MAX_PAYLOAD_SIZE must reflect the size of COAP_BLOCK_x. This is
necessary becase BLOCK size represents max payload size. The current
value create inconsistencies for coap lib. The same way,
MAX_DOWNLOAD_DATA must allocate sufficient space for MAX_PAYLOAD_SIZE
plus all space for coap header etc.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
2020-03-30 16:54:29 +03:00
Gerson Fernando Budke
92f9cd9f85 lib: updatehub: Fix build warnings
Fix all build warnings.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
2020-03-30 16:54:29 +03:00
Måns Ansgariusson
2f323e65b2 lib: cmsis_rtos: Fix Periodic timer
This commit changes the behaviour of the CMSIS-RTOS periodic timers to
have an initial timeout equal to the periodic timeout instead of
executing the callback function directly when calling the
osTimerStart(...); function.

This behavioural change is according to the CMSIS-RTOS specification.

Signed-off-by: Måns Ansgariusson <Mans.Ansgariusson@AssaAbloy.com>
2020-03-25 09:37:59 -04:00
Carles Cufi
d212bc4d60 ext: lib: Move fnmatch to lib/
Since we already have similarly licensed 3-clause BSD files in the tree,
and in particular in our minimal libc, move the fnmatch functionality
from ext/ to lib/.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2020-03-20 22:56:19 -04:00
Carles Cufi
4b37a8f3a4 Revert "global: Replace BUILD_ASSERT_MSG() with BUILD_ASSERT()"
This reverts commit 8739517107.

Pull Request #23437 was merged by mistake with an invalid manifest.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2020-03-19 18:45:13 +01:00
Oleg Zhurakivskyy
8739517107 global: Replace BUILD_ASSERT_MSG() with BUILD_ASSERT()
Replace all occurences of BUILD_ASSERT_MSG() with BUILD_ASSERT()
as a result of merging BUILD_ASSERT() and BUILD_ASSERT_MSG().

Signed-off-by: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
2020-03-19 15:47:53 +01:00
Julien D'Ascenzio
1128c65ab0 lib: posix: fix ARG_UNUSED check on clock_getrtc
The ARG_UNUSED should be on tz, not tv

Signed-off-by: Julien D'Ascenzio <julien.dascenzio@paratronic.fr>
2020-03-18 12:28:43 -04:00
Andrew Boie
2dc2ecfb60 kernel: rename struct _k_object
Private type, internal to the kernel, not directly associated
with any k_object_* APIs. Is the return value of z_object_find().
Rename to struct z_object.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2020-03-17 20:11:27 +02:00
Andrew Boie
f2734ab022 kernel: use a union for kobject data values
Rather than stuffing various values in a uintptr_t based on
type using casts, use a union for this instead.

No functional difference, but the semantics of the data member
are now much clearer to the casual observer since it is now
formally defined by this union.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2020-03-17 20:11:27 +02:00
Nicholas Lowell
17b19eb810 pthread: add setname/getname glibc extensions
Adding the ability to set and get pthread names by defining
some non-standard extension functions that were first
introduced by Glibc.

Similar to zephyr thread naming, these allow for thread
tracking and debugging even when using the more portable
posix API.

Though Glibc was the originator, the current POSIX functions
have return codes based on Oracle's adopted spec, so these
functions follow suit.  The Oracle and Glibc function
prototypes match.

Signed-off-by: Nicholas Lowell <nlowell@lexmark.com>
2020-03-10 15:15:34 +02:00
Flavio Ceolin
9e45c67679 lib: posix: Fix undefined reference to clock_gettime()
timespec_to_timeoutms calls clock_gettime that requires
CONFIG_POSIX_CLOCK. ifdef this function to avoid undefined reference.

Fixes #20137

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2020-03-10 14:57:16 +02:00
Jukka Rissanen
9d4fbb2912 crc: Add crc8 implementation and tests
Add crc8 implementation and unit tests for it.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2020-03-10 12:53:53 +02:00
Nick Ward
1e503b5ca7 lib: posix: clock: Fix nanosecond rollover logic
It was out by one count.

Signed-off-by: Nick Ward <nix.ward@gmail.com>
2020-02-15 23:09:43 +03:00
Ulf Magnusson
eddd98f811 kconfig: Replace some single-symbol 'if's with 'depends on'
I think people might be reading differences into 'if' and 'depends on'
that aren't there, like maybe 'if' being needed to "hide" a symbol,
while 'depends on' just adds a dependency.

There are no differences between 'if' and 'depends on'. 'if' is just a
shorthand for 'depends on'. They work the same when it comes to creating
implicit menus too.

The way symbols get "hidden" is through their dependencies not being
satisfied ('if'/'depends on' get copied up as a dependency on the
prompt).

Since 'if' and 'depends on' are the same, an 'if' with just a single
symbol in it can be replaced with a 'depends on'. IMO, it's best to
avoid 'if' there as a style choice too, because it confuses people into
thinking there's deep Kconfig magic going on that requires 'if'.

Going for 'depends on' can also remove some nested 'if's, which
generates nicer symbol information and docs, because nested 'if's really
are so simple/dumb that they just add the dependencies from both 'if's
to all symbols within.

Replace a bunch of single-symbol 'if's with 'depends on' to despam the
Kconfig files a bit and make it clearer how things work. Also do some
other minor related dependency refactoring.

The replacement isn't complete. Will fix up the rest later. Splitting it
a bit to make it more manageable.

(Everything above is true for choices, menus, and comments as well.)

Detected by tweaking the Kconfiglib parsing code. It's impossible to
detect after parsing, because 'if' turns into 'depends on'.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2020-02-08 08:32:42 -05:00
Andrew Boie
efc5fe07a2 kernel: overhaul unused stack measurement
The existing stack_analyze APIs had some problems:

1. Not properly namespaced
2. Accepted the stack object as a parameter, yet the stack object
   does not contain the necessary information to get the associated
   buffer region, the thread object is needed for this
3. Caused a crash on certain platforms that do not allow inspection
   of unused stack space for the currently running thread
4. No user mode access
5. Separately passed in thread name

We deprecate these functions and add a new API
k_thread_stack_space_get() which addresses all of these issues.

A helper API log_stack_usage() also added which resembles
STACK_ANALYZE() in functionality.

Fixes: #17852

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2020-02-08 10:02:35 +02:00
Kumar Gala
4fb265657d libc: newlibc: Implement dummy _gettimeofday to link
With the change in SDK 0.11.1 to newlib to remove
-DMISSING_SYSCALL_NAMES we now need to implement a version of
_gettimeofday.  Previously with pre SDK 0.11.1 we had a recursive mess
of _gettimeofday_r -> gettimeofday -> _gettimeofday_r.  (which are all
implemented in newlib and thus we didn't get a link error).

With SDK 0.11.1 we have: _gettimeofday_r -> _gettimeofday.  And we
should provide a version of _gettimeofday.

Fixes #22484

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-02-05 20:47:41 +01:00
Kumar Gala
c8696293a9 libc: fix build issue with posix fs APIs on xtensa
On xtensa we always need to implement the reentrant fs syscall
functions.  So remove the #ifndef CONFIG_POSIX_API protection around
them and add needed externs.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-02-04 20:59:52 +02:00
Kumar Gala
9897c3b0dd libc: Move xtensa reentrant syscall impl to common libc-hooks
The xcc specific reentrant syscall implementations are actually useful
for xtensa in general.  So move that code from being specific to
intel_s1000 / xcc into generic newlib/libc-hooks.c.  This is in prep
for the Zephyr SDK dropping -DMISSING_SYSCALL_NAMES which will make
its version of newlib on xtensa match behavior with xcc.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-02-03 14:57:10 -06:00
Maureen Helm
514b5861c3 gui: Add support for lvgl touch input device
Adds support for an optional lvgl touch input device using the zephyr
keyboard scan interface. This can be used with the ft5336 touch panel
driver, which returns single touch coordinates through the kscan
driver callback.

Signed-off-by: Maureen Helm <maureen.helm@nxp.com>
2020-02-01 08:50:16 -05:00
Kumar Gala
6317c82f06 toolchain: Have Kconfig NEWLIB_LIBC_NANO depend on toolchain support
Introduce HAS_NEWLIB_LIBC_NANO Kconfig option that the toolchain
specific Kconfig (gnuarmemb & zephyr 0.11) can select to convey that the
feature is supported.

This removes the need to if protect the NEWLIB_LIBC_NANO Kconfig with:

    if "$(ZEPHYR_TOOLCHAIN_VARIANT)" = "gnuarmemb"

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-01-29 12:22:31 -06:00
Peter A. Bigot
1964bf08bb lib: os: onoff: add API for on-off service request and release management
There are various situations where it's necessary to support turning
devices on or off at runtime, includin power rails, clocks, other
peripherals, and binary device power management.  The complexity of
properly managing multiple consumers of a device in a multithreaded
system suggests that a shared implementation is desirable.  This
commit provides an API that supports managing on-off resources.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2020-01-29 14:08:46 +01:00
Ulf Magnusson
a094cafecc gui: kconfig: Fix broken refs to LVGL_OBJ_PRELOAD_DEF_ANIMATION_*
The CONFIG_ prefixes were missing.

Found with a work-in-progress scripts/kconfig/lint.py check.

These are defined in lib/gui/lvgl/Kconfig.objects.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2020-01-25 08:15:17 -05:00
Maureen Helm
ff1cd6e10c gui: Add BGR565 pixel format support to lvgl
Adds support for the BGR565 pixel format to lvgl. This fixes the lvgl
sample for mimxrt10{50,60,64}_evk boards, which were broken when the
mcux elcdif display driver was modified in commit
9041b0f119 to return the BGR565 pixel
format instead of RGB565.

Signed-off-by: Maureen Helm <maureen.helm@nxp.com>
2020-01-22 08:55:16 +01:00
Robert Lubos
665d195f3a net: sockets: tls: Add missing symbols for a few options
`TLS_PEER_VERIFY` and `TLS_DTLS_ROLE` options accept specific values,
yet no symbols were defined for them. In result, magic numbers were used
in several places, making the code less readable.

Fix this issue, by adding the missing symbols to the `socket.h` header,
and using them in places where related socket options are set.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2020-01-20 09:58:23 +02:00
Joakim Andersson
27bbfb66b4 assert: Completely remove file info and condition expression
Completely remove the file info and condition expression from the
the print statement if they are not enabled. This saves a little code
space which adds up when there are many assert calls.

In bluetooth shell test this saves around 4.5k bytes.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-13 13:59:55 +01:00
Andrew Boie
d76ae46c0c lib: os: make snprintk fns generally available
The intention of disabling CONFIG_PRINTK is that all
invocations of it will compile to nothing, saving a lot
of runtime overhead and footprint since all the format
strings are completely dropped; instances of printk()
and related functions are no-ops.

However, some subsystems need snprintk() for string
processing, since the snprintf() implementations in even
minimal C library are too costly in text footprint or
stack usage for some applications. This processing is
required for the application to even function.

This patch continues to have disabling  CONFIG_PRINTK to
cause the non snprintk functions to become no-ops, but
now we always compile the necessary bits for snprintk(),
relying on gc-sections to discard them if unused.

z_vprintk() is now unconditionally defined in the header
since it is not tied to any particular output sink and
is intended for users who know exactly what they are
doing (it's in zephyr private scope).

Relates to: #21564

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2020-01-03 10:13:30 +01:00
Pavlo Hamov
03baef9975 libc: syscall: make aliases WEAK by default
make all syscall functions overridable

Signed-off-by: Pavlo Hamov <pavlo_hamov@jabil.com>
2019-12-17 10:35:20 -08:00
Andrew Boie
c5e3688583 lib: os: don't cast mutex pointers to u32_t
Just use the correct data type.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-12-12 14:48:42 -08:00
Josh Gao
c0026346a3 libc: permit users to supply their own malloc.
Severely memory constrained systems with known allocation patterns can
benefit from providing their own implementation of malloc with
specifically tuned bucket sizes. Provide a switch to allow users to
replace the default malloc implementation with their own.

Signed-off-by: Josh Gao <josh@jmgao.dev>
2019-12-12 10:49:52 -06:00
Ulf Magnusson
984bfae831 global: Remove leading/trailing blank lines in files
Remove leading/trailing blank lines in .c, .h, .py, .rst, .yml, and
.yaml files.

Will avoid failures with the new CI test in
https://github.com/zephyrproject-rtos/ci-tools/pull/112, though it only
checks changed files.

Move the 'target-notes' target in boards/xtensa/odroid_go/doc/index.rst
to get rid of the trailing blank line there. It was probably misplaced.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-12-11 19:17:27 +01:00
Jan Van Winkel
a1e336e7af gui: Added support for LVGL v6.1
Added support for new features in LVGL v6.1

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
2019-12-09 15:07:28 -06:00
Andrew Boie
e794da070a lib: os: uncrustify sem.c
Also fix a spelling error.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-11-18 13:52:15 +01:00
Andy Ross
8892406c1d kernel/sys_clock.h: Deprecate and convert uses of old conversions
Mark the old time conversion APIs deprecated, leave compatibility
macros in place, and replace all usage with the new API.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-11-08 11:08:58 +01:00
Andrew Boie
4f77c2ad53 kernel: rename z_arch_ to arch_
Promote the private z_arch_* namespace, which specifies
the interface between the core kernel and the
architecture code, to a new top-level namespace named
arch_*.

This allows our documentation generation to create
online documentation for this set of interfaces,
and this set of interfaces is worth treating in a
more formal way anyway.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-11-07 15:21:46 -08:00
Andrew Boie
ec3aafbf78 printk: print pointers on 64-bit properly
Needs a min-width of 16, not 8, for 64-bit.
Some indentation oddities fixed.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-11-06 17:50:34 -08:00
Stephanos Ioannidis
2d7460482d headers: Refactor kernel and arch headers.
This commit refactors kernel and arch headers to establish a boundary
between private and public interface headers.

The refactoring strategy used in this commit is detailed in the issue

This commit introduces the following major changes:

1. Establish a clear boundary between private and public headers by
  removing "kernel/include" and "arch/*/include" from the global
  include paths. Ideally, only kernel/ and arch/*/ source files should
  reference the headers in these directories. If these headers must be
  used by a component, these include paths shall be manually added to
  the CMakeLists.txt file of the component. This is intended to
  discourage applications from including private kernel and arch
  headers either knowingly and unknowingly.

  - kernel/include/ (PRIVATE)
    This directory contains the private headers that provide private
   kernel definitions which should not be visible outside the kernel
   and arch source code. All public kernel definitions must be added
   to an appropriate header located under include/.

  - arch/*/include/ (PRIVATE)
    This directory contains the private headers that provide private
   architecture-specific definitions which should not be visible
   outside the arch and kernel source code. All public architecture-
   specific definitions must be added to an appropriate header located
   under include/arch/*/.

  - include/ AND include/sys/ (PUBLIC)
    This directory contains the public headers that provide public
   kernel definitions which can be referenced by both kernel and
   application code.

  - include/arch/*/ (PUBLIC)
    This directory contains the public headers that provide public
   architecture-specific definitions which can be referenced by both
   kernel and application code.

2. Split arch_interface.h into "kernel-to-arch interface" and "public
  arch interface" divisions.

  - kernel/include/kernel_arch_interface.h
    * provides private "kernel-to-arch interface" definition.
    * includes arch/*/include/kernel_arch_func.h to ensure that the
     interface function implementations are always available.
    * includes sys/arch_interface.h so that public arch interface
     definitions are automatically included when including this file.

  - arch/*/include/kernel_arch_func.h
    * provides architecture-specific "kernel-to-arch interface"
     implementation.
    * only the functions that will be used in kernel and arch source
     files are defined here.

  - include/sys/arch_interface.h
    * provides "public arch interface" definition.
    * includes include/arch/arch_inlines.h to ensure that the
     architecture-specific public inline interface function
     implementations are always available.

  - include/arch/arch_inlines.h
    * includes architecture-specific arch_inlines.h in
     include/arch/*/arch_inline.h.

  - include/arch/*/arch_inline.h
    * provides architecture-specific "public arch interface" inline
     function implementation.
    * supersedes include/sys/arch_inline.h.

3. Refactor kernel and the existing architecture implementations.

  - Remove circular dependency of kernel and arch headers. The
   following general rules should be observed:

    * Never include any private headers from public headers
    * Never include kernel_internal.h in kernel_arch_data.h
    * Always include kernel_arch_data.h from kernel_arch_func.h
    * Never include kernel.h from kernel_struct.h either directly or
     indirectly. Only add the kernel structures that must be referenced
     from public arch headers in this file.

  - Relocate syscall_handler.h to include/ so it can be used in the
   public code. This is necessary because many user-mode public codes
   reference the functions defined in this header.

  - Relocate kernel_arch_thread.h to include/arch/*/thread.h. This is
   necessary to provide architecture-specific thread definition for
   'struct k_thread' in kernel.h.

  - Remove any private header dependencies from public headers using
   the following methods:

    * If dependency is not required, simply omit
    * If dependency is required,
      - Relocate a portion of the required dependencies from the
       private header to an appropriate public header OR
      - Relocate the required private header to make it public.

This commit supersedes #20047, addresses #19666, and fixes #3056.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2019-11-06 16:07:32 -08:00
Nicolas Pitre
132b2b8c99 mempool: trap on double-free instances
A double-free could cause very hard to find bugs when using the mempool
allocator as the same memory would end up being allocated twice
afterwards.

Now that bits in the block bitmap are cleared only when actually freeing
a block, we may simply ensure those bits are still set before clearing
them, effectively catching most double-free cases.

The alloc_bit_is_set() function is made static inline so that when
assertion checks are disabled the compiler won't complain about unused
code.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-11-06 21:42:42 +01:00
Ulf Magnusson
39f2281c89 kconfig: Change some '#Comment' comments to '# Comment'
For consistency. Newly-introduced stuff.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-11-04 17:31:27 -05:00
Ulf Magnusson
bd6e04411e kconfig: Clean up header comments and make them consistent
Use this short header style in all Kconfig files:

    # <description>

    # <copyright>
    # <license>

    ...

Also change all <description>s from

    # Kconfig[.extension] - Foo-related options

to just

    # Foo-related options

It's clear enough that it's about Kconfig.

The <description> cleanup was done with this command, along with some
manual cleanup (big letter at the start, etc.)

    git ls-files '*Kconfig*' | \
        xargs sed -i -E '1 s/#\s*Kconfig[\w.-]*\s*-\s*/# /'

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-11-04 17:31:27 -05:00
Ulf Magnusson
8cb26d19c6 libc: kconfig: Have MINIMAL_LIBC depend on !REQUIRES_FULL_LIBC
This prevents MINIMAL_LIBC from being selected by the user (in the
menuconfig or in a configuration file) when REQUIRES_FULL_LIBC is y.
'default' on a choice only determines the default selection, not what
symbols can be selected.

It's helpful to think of Kconfig in terms of someone going into the
menuconfig and making changes.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-11-02 00:31:57 +01:00
Ulf Magnusson
975de21858 kconfig: Global whitespace/consistency cleanup
Clean up space errors and use a consistent style throughout the Kconfig
files. This makes reading the Kconfig files more distraction-free, helps
with grepping, and encourages the same style getting copied around
everywhere (meaning another pass hopefully won't be needed).

Go for the most common style:

 - Indent properties with a single tab, including for choices.

   Properties on choices work exactly the same syntactically as
   properties on symbols, so not sure how the no-indentation thing
   happened.

 - Indent help texts with a tab followed by two spaces

 - Put a space between 'config' and the symbol name, not a tab. This
   also helps when grepping for definitions.

 - Do '# A comment' instead of '#A comment'

I tweaked Kconfiglib a bit to find most of the stuff.

Some help texts were reflowed to 79 columns with 'gq' in Vim as well,
though not all, because I was afraid I'd accidentally mess up
formatting.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-11-01 15:53:23 +01:00
Jan Van Winkel
9555f82d28 libc: Use select REQUIRES_FULL_LIBC instead of select NEWLIB_LIBC
Changed select NEWLIB_LIBC to select REQUIRES_FULL_LIBC

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
2019-11-01 14:18:36 +01:00
Jan Van Winkel
0e59f6846f libc: Changed dependencies of NEWLIB_C into !MINIMAL_LIBC
Changed Kconfig dependencies of NEWLIB_C into !MINIMAL_LIBC

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
2019-11-01 14:18:36 +01:00
Jan Van Winkel
c1f865b412 libc: Added Kconfig choice to select C library
Added Kconfig choice to select C library implementation

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
2019-11-01 14:18:36 +01:00
Anas Nashif
8d22fd9263 updatehub: move header to library
Not a top-level zephyr core API and tied to third party environment, so
move it to where the code is in lib/updatehub.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-10-25 18:20:20 -04:00
Daniel Leung
b7eb04b300 x86: consolidate x86_64 architecture, SoC and boards
There are two set of code supporting x86_64: x86_64 using x32 ABI,
and x86 long mode, and this consolidates both into one x86_64
architecture and SoC supporting truly 64-bit mode.

() Removes the x86_64:x32 architecture and SoC, and replaces
   them with the existing x86 long mode arch and SoC.
() Replace qemu_x86_64 with qemu_x86_long as qemu_x86_64.
() Updates samples and tests to remove reference to
   qemu_x86_long.
() Renames CONFIG_X86_LONGMODE to CONFIG_X86_64.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2019-10-25 17:57:55 -04:00
Ulf Magnusson
41a0e762ed lib: cmsis_v1: kconfig: Remove unused CMSIS_MAX_THREAD_COUNT symbol
Added in commit ccd1c21824 ("lib/cmsis_rtos_v1: Implement support for
thread APIs"), then never used.

Found with a script.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-10-22 23:02:16 -04:00
Ulf Magnusson
01aa3117ac gui: lvgl: Fix broken CONFIG_LVGL_OBJ_WINDOW check
Was impossible to enable due to a typo. Fix it.

Found with a script (LVGL_OBJ_WINDOW was unused besides
being enabled in tests/lib/gui/lvgl/prj.conf).

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-10-22 11:09:51 +02:00
Peter Bigot
66c8756956 coccinelle: standardize kernel API timeout arguments
Re-run with updated script to detect missed cases.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2019-10-04 16:23:57 -04:00
Nicolas Pitre
bb7c2e82b1 mempool: remove redundant bit set/clear within loops
When small blocks are recombined to create a single block at a shallower
level, it is sufficient to remove those blocks from the free list. There
is no need to mark those small blocks as allocated in the bitmap.

This, in turn, removes the need to mark small blocks back as unallocated
when splitting up a big blocks as they'll already be so marked.
Only the first small block needs to be marked allocated and the
remaining blocks only need to be added to the free list.

This makes the code smaller and more efficient, especially since those
removed bit manipulations were located within loops.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-10-04 13:42:59 -04:00
Nicolas Pitre
1b193e9ece mempool: reverse free bit semantic
This turns the free-bit flag into an alloc-bit flag effectively
reversing its semantic. This is to make further changes more natural
and easier to understand.

No need to clear the alloc bits at init time as they're located in .bss
and all clear already.

The code remains functionally equivalent after this change.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-10-04 13:42:59 -04:00
Peter Bigot
ab91eef23b coccinelle: standardize kernel API timeout arguments
Use the int_literal_to_timeout Coccinelle script to convert literal
integer arguments for kernel API timeout parameters to the standard
timeout value representations.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2019-10-03 11:55:44 -07:00
Andrew Boie
fe031611fd kernel: rename main/idle thread/stacks
The main and idle threads, and their associated stacks,
were being referenced in various parts of the kernel
with no central definition. Expose these in kernel_internal.h
and namespace with z_ appropriately.

The main and idle threads were being defined statically,
with another variable exposed to contain their pointer
value. This wastes a bit of memory and isn't accessible
to user threads anyway, just expose the actual thread
objects.

Redundance MAIN_STACK_SIZE and IDLE_STACK_SIZE defines
in init.c removed, just use the Kconfigs they derive
from.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-09-30 15:25:55 -04:00
Andrew Boie
4ad9f687df kernel: rename thread return value functions
z_set_thread_return_value is part of the core kernel -> arch
interface and has been renamed to z_arch_thread_return_value_set.

z_set_thread_return_value_with_data renamed to
z_thread_return_value_set_with_data for consistency.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-09-30 15:25:55 -04:00
Nicolas Pitre
2129937d3d realloc(): move mempool internal knowledge out of generic lib code
The realloc function was a bit too intimate with the mempool accounting.
Abstract that knowledge away and move it where it belongs.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-09-30 10:57:24 -07:00
Anas Nashif
50d5e37b8a tests: move util test to be unit tests
Move to a unit test, no need to build this for every platform we have.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-09-27 15:23:26 -04:00
Kumar Gala
4cbe5c0961 lib/libc/min: Introduce simple math.h
Introduce math.h to get definitions of float_t and double_t.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2019-09-27 04:30:07 -07:00
Sebastian Bøe
8610b84d55 cmake: don't link interface libraries with zephyr_interface
Stop linking interface libraries against zephyr_interface. This is
cargo cult code that in practice does nothing.

Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
2019-09-26 05:50:46 -07:00
Paul Sokolovsky
0173d86e6f lib: posix, minlibc: struct itimerspec is defined by sys/timespec.h
Newlib has it defined in sys/timespec.h, and thus per the established
conventions, everything else relies on it being there. Specifically,
minimal libc acquires sys/timespec.h with a similar definition, and
POSIX headers rely on that header. Still with a workaround for old
Newlib version as used by Xtensa (but all infrastructure for that is
already there; actually, this patch removes duplicate similar-infra,
which apparently didn't work as expected by now, so now we have a
single workaround, not 2 different once).

To emphasize a point, now there 2 headers:

sys/_timespec.h, defining struct timespec, and
sys/timespec.h, defining struct itimerspec

That's how Newlib has it, and what we faithfully embrace and follow,
because otherwise, there will be header conflicts depending on
various libc and POSIX subsys options.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-09-25 20:56:29 -07:00
Paul Sokolovsky
0dcee73fe1 lib: posix: Kconfig: Allow to enable individual components
Allow to enable individual POSIX components, like Pthreads.
CONFIG_POSIX_API now just enables all of individual POSIX components,
and sets up environment suitable to easily port POSIX applications to
Zephyr.

Fixes: #12965

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-09-25 20:56:29 -07:00
Kim Sekkelund
0450263393 Bluetooth: Host: Remove printk dependency from settings
Some modules use snprintk to format the settings keys. Unfortunately
snprintk is tied with printk which is very large for some embedded
systems.
To be able to have settings enabled without also enabling printk
support, change creation of settings key strings to use bin2hex, strlen
and strcpy instead.
A utility function to make decimal presentation of a byte value is
added as u8_to_dec in lib/os/dec.c
Add new Kconfig setting BT_SETTINGS_USE_PRINTK

Signed-off-by: Kim Sekkelund <ksek@oticon.com>
2019-09-25 17:36:39 +02:00
Peter A. Bigot
55ace13c32 lib/timeutil: avoid implementation-defined behavior
The algorithm for converting broken-down civil time to seconds in the
POSIX epoch time scale would produce undefined behavior on a toolchain
that uses a 32-bit time_t in cases where the referenced time could not
be represented exactly.

However, there are use cases in Zephyr for civil time conversions
outside the 32-bit representable range of 1901-12-13T20:45:52Z through
2038-01-19T03:14:07Z inclusive.

Add new API that specifically returns a 64-bit signed seconds count, and
revise the existing API to detect out-of-range values and convert them
to a diagnosible error.

Closes #18465

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2019-09-19 20:49:51 -04:00
Peter A. Bigot
cc1594a59a lib/timeutil: support const correctness for pointer parameter
timeutil_timegm() does not modify the passed structure, so it should
indicate that in the signature (even though the GNU extension does not).

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2019-09-19 20:49:51 -04:00
Peter Bigot
dfd26dbbeb libc/minimal/assert: conditionalize static_assert macro
static_assert was not added to C until C11.  Zephyr builds default to
C99.  To preserve compatibility with newlib avoid defining the
macro at standard levels where it did not exist.

Relates to #17738 and #11754.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2019-09-19 09:37:42 -05:00
Andy Ross
643701aaf8 kernel: syscalls: Whitespace fixups
The semi-automated API changes weren't checkpatch aware.  Fix up
whitespace warnings that snuck into the previous patches.  Really this
should be squashed, but that's somewhat difficult given the structure
of the series.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-09-12 11:31:50 +08:00
Andy Ross
346cce31d8 kernel: Port remaining buildable syscalls to new API
These calls are buildable on common sanitycheck platforms, but are not
invoked at runtime in any tests accessible to CI.  The changes are
mostly mechanical, so the risk is low, but this commit is separated
from the main API change to allow for more careful review.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-09-12 11:31:50 +08:00
Andy Ross
6564974bae userspace: Support for split 64 bit arguments
System call arguments, at the arch layer, are single words.  So
passing wider values requires splitting them into two registers at
call time.  This gets even more complicated for values (e.g
k_timeout_t) that may have different sizes depending on configuration.
This patch adds a feature to gen_syscalls.py to detect functions with
wide arguments and automatically generates code to split/unsplit them.

Unfortunately the current scheme of Z_SYSCALL_DECLARE_* macros won't
work with functions like this, because for N arguments (our current
maximum N is 10) there are 2^N possible configurations of argument
widths.  So this generates the complete functions for each handler and
wrapper, effectively doing in python what was originally done in the
preprocessor.

Another complexity is that traditional the z_hdlr_*() function for a
system call has taken the raw list of word arguments, which does not
work when some of those arguments must be 64 bit types.  So instead of
using a single Z_SYSCALL_HANDLER macro, this splits the job of
z_hdlr_*() into two steps: An automatically-generated unmarshalling
function, z_mrsh_*(), which then calls a user-supplied verification
function z_vrfy_*().  The verification function is typesafe, and is a
simple C function with exactly the same argument and return signature
as the syscall impl function.  It is also not responsible for
validating the pointers to the extra parameter array or a wide return
value, that code gets automatically generated.

This commit includes new vrfy/msrh handling for all syscalls invoked
during CI runs.  Future commits will port the less testable code.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-09-12 11:31:50 +08:00
Kumar Gala
749ca106a0 libc: Make libc-nano default for ARM toolchain if newlib
The ARM embedded toolchain has 2 newlib based libc build variants, one
that utilizes the "nano" configuration which is more in line with the
Zephyr SDK.  Make the "nano" cfg the default if newlib is enabled to
match closer how the Zephyr SDK behaves.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2019-08-28 10:59:29 -05:00
Paul Sokolovsky
0906671a7b posix: pthread: pthread_mutex_timedlock should accept absolute deadline
It was coded as if it accepts relative timeout. Normative reference:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_timedlock.html

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-08-27 17:50:48 -04:00
Paul Sokolovsky
68c7dc6b96 posix: pthread: pthread_cond_timedwait should accept absolute deadline
Instead, it was coded as if it accepted a relative timeout. Normative
reference:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html

Fixes: #17812

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-08-27 17:50:48 -04:00
Paul Sokolovsky
6c5a08899a posix: fs: ZFD_IOCTL_CLOSE: Be sure to call posix_fs_free_obj()
To make sure that entry in fs.c:desc_array[] is freed. Note that
freeing an entry in fdtable is handled by generic implementation
of close().

Fixes: #17231

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-08-27 10:34:54 -04:00
Andrew Boie
aed767a98c libc: newlib: make sbrk() thread-safe
Concurrent use of this function could lead to corruption.
Use a sys_sem to synchronize access.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-08-22 07:38:56 -05:00
Paul Sokolovsky
5b3df8a180 posix: pthread_create: Ignore retval of pthread_mutex_init() calls
pthread_mutex_init() just redirects to Zephyr kernel primitive, for
initializing structure fields. So, use the knowledge that it can't
fail (for as long as structure pointer is initialized, and here it's
from pre-allocated array), and ignore return value of
pthread_mutex_init()

Coverity-CID: 203542
Fixes: #18371

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-08-21 07:26:59 -05:00
Peter Bigot
96c1b05125 lib/newlib: revert treatment of libc files as system includes
The solution from #14312 of using -isystem to prioritize the position of
the libc directory bypasses the effect of -ffreestanding with respect to
libc symbols expected to be present in a non-hosted environment.

Further, it breaks C++ with the ARM Embedded toolchain as the system
fails to find the right file with #include_next.

Use a more fine-grained solution that explicitly includes the underlying
newlib header required for <inttypes.h> support before moving on to
include the next available one, whether system or non-system.

Closes #17564

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2019-08-20 12:51:16 -05:00
Peter Bigot
849df51666 lib/libc: rearrange for standard use of extern "C"
Consistently place C++ use of extern "C" after all include directives,
within the negative branch of _ASMLANGUAGE if used.

Background from issue #17997:

Declarations that use C linkage should be placed within extern "C"
so the language linkage is correct when the header is included by
a C++ compiler.

Similarly #include directives should be outside the extern "C" to
ensure the language-specific default linkage is applied to any
declarations provided by the included header.

See: https://en.cppreference.com/w/cpp/language/language_linkage
Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2019-08-19 23:36:59 +02:00
Peter Bigot
b6dafeb612 include/lvgl: rearrange for standard use of extern "C"
Consistently place C++ use of extern "C" after all include directives,
within the negative branch of _ASMLANGUAGE if used.

Background from issue #17997:

Declarations that use C linkage should be placed within extern "C"
so the language linkage is correct when the header is included by
a C++ compiler.

Similarly #include directives should be outside the extern "C" to
ensure the language-specific default linkage is applied to any
declarations provided by the included header.

See: https://en.cppreference.com/w/cpp/language/language_linkage
Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2019-08-13 18:00:31 +02:00
Jan Van Winkel
55c3585f18 gui: Corrected paths in Zephyr to LVGL FS mapping
Corrected path handling between zephyr and LVGL FS API

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
2019-08-09 07:35:38 -05:00
Jan Van Winkel
fa27e583a1 gui: Disable LVGL features by default
Removed 'default y' from LVGL Kconfig files to disable features by
default

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
2019-08-09 07:35:38 -05:00
Jan Van Winkel
6bbd4cbaa3 gui: Add support for lvgl API version 6
Added support for lvgl API version 6

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
2019-08-09 07:35:38 -05:00
Peter A. Bigot
ef4cd6a1ab lib/libc/minimal: Refined handle duplicate time definitions
Follow the approach of newlib to use a file sys/_types.h to specify the
underlying type for POSIX/libc types that must be provided in multiple
headers.  The identifier for this type is in the reserved namespace.

Use this type rather than a specific standard type in all headers that
need to provide the type under its public name.

Remove the inclusion of <sys/types.h> from headers that should not bring
in all symbols present in that header, replacing it with the standard
boilerplate to expose the specific symbols that are required.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2019-08-09 00:43:09 -07:00
Kumar Gala
daf9f11024 lib/libc/min: Handle duplicate time definitions
time_t and suseconds_t are defined in time.h and sys/types.h.  Handle
the duplication by adding ifdef protection around them similar to what
is being done for other types.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2019-08-09 00:43:09 -07:00
Paul Sokolovsky
f0d483aaa6 lib: posix: Switch to use zephyr_interface_library_named cmake directive
Similar to how other sub-libraries are defined in Zephyr tree, e.g.
"fs", "lgvl", etc. This is supposed to help with the need to
explicitly add posix include path to each and every application using
POSIX subsys.

Fixes: #15627

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-08-08 14:31:35 +02:00
Paul Sokolovsky
7d2df83b51 libc: minimal: Add headers as system includes
This is consistent with how newlib headers are treated, and will
have effect of ninlibc headers to be further down in the include
order. This is important, because some POSIX subsys headers
override those of libc. Without this change, we can't streamline
POSIX build config using zephyr_interface_library_named() cmake
directive, because includes will be in wrong order.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-08-08 14:31:35 +02:00
Paul Sokolovsky
ff6432edad lib: posix: Use "posix_subsys" as the CMake lib for the subsystem.
Historically, it used to be "PTHREAD", which is no longer true, as
POSIX subsys offers much more functionality than just Pthreads. Use
detailed name, like "posix_subsys", to avoid possible confusion with
ARCH_POSIX-related matters.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-08-08 14:31:35 +02:00
Nicolas Pitre
1f4b5ddd0f riscv32: rename to riscv
With the upcoming riscv64 support, it is best to use "riscv" as the
subdirectory name and common symbols as riscv32 and riscv64 support
code is almost identical. Then later decide whether 32-bit or 64-bit
compilation is wanted.

Redirects for the web documentation are also included.

Then zephyrbot complained about this:

"
New files added that are not covered in CODEOWNERS:

dts/riscv/microsemi-miv.dtsi
dts/riscv/riscv32-fe310.dtsi

Please add one or more entries in the CODEOWNERS file to cover
those files
"

So I assigned them to those who created them. Feel free to readjust
as necessary.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-08-02 13:54:48 -07:00
Peter A. Bigot
8420f43b86 libc: minimal: add strspn and strcspn support
These functions are useful for determining prefixes, as with file system
paths.  They are required by littlefs.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2019-07-31 09:22:49 -07:00
Peter A. Bigot
b8af1a6a4e libc/minimal: fix reproducibility of gmtime
struct tm has fields that were not being set by the implementation,
causing the test to fail when the uninitialized values were compared
with a static initialized result.  Zero the structure before filling it.

Closes #17794

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2019-07-31 11:48:18 +03:00
Paul Sokolovsky
6b90a02bb4 libc: minimal: time.h: Don't (re)define struct timespec.
By the latest convention, libc's define struct timespec in
sys/_timespec.h. This is consistent with Newlib and ensures
about errors due to redefinitions.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-07-25 13:50:58 -04:00
Paul Sokolovsky
b7bb48eeac posix: unistd.h: open() doesn't belong here
Per POSIX, open() is defined in <fcntl.h>. fcntl.h in turn comes from
the underlying libc, either newlib, or minimal libc.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-07-25 13:50:58 -04:00
Paul Sokolovsky
1347bf9b48 posix: mqueue.h: Move O_CREAT and friends to fcntl.h
That's the header which is supposed to define them, there was even
FIXME on that in mqueue.h.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-07-25 13:50:58 -04:00
Paul Sokolovsky
3a4553913e posix: struct timespec: Move definition to sys/_timespec.h
POSIX subsys defines struct timespec in <time.h> (as POSIX public
API requires), but newlib defines in in sys/_timespec.h, which
inevitably leads to inclusion order and redifinition conflicts.
Follow newlib way and define it in single place, sys/_timespec.h,
which belongs to libc namespace. Thus, we move current definition
to minimal libc, and will use either minlibc's or newlib's
definition, instead of trying to redefine it.

This is similar to the introduction of sys/_timeval.h done earlier.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-07-25 13:50:58 -04:00
Paul Sokolovsky
3693f85805 posix: Move sys/stat.h to minimal libc
Newlib libc already provides sys/stat.h, so trying to have sys/stat.h
on the level of POSIX subsys inevitable leads to include order and
definition conflicts. Instead (as most of other sys/* includes)
should come from the underlying libc.

While moving, made unrelated change of removing #include <kernel.h>,
to accommodate the change reviewers.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-07-25 13:50:58 -04:00
Wentong Wu
715369350d lib: os: add sys_sem data type
For systems with userspace, the sys_sem exist in user memory working
as counter semaphore for user mode thread. The implemention of sys_sem
is based on k_futex. And the majority of the synchronization operations
are performed in user mode to reduce the calling of system call.
And for systems without userspace enabled, sys_sem behaves like k_sem.

Fixes: #15139.

Signed-off-by: Wentong Wu <wentong.wu@intel.com>
2019-07-24 10:12:25 -07:00
Andrew Boie
39425eaada assert: generate oops if invoked from usermode
User mode isn't allowed to generate a panic and this would
lead to a confusing privilege violation exception.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-07-20 08:29:39 -04:00
Peter A. Bigot
9d25b671bc sys: timeutil: add module
Add a generic API to provide the inverse operation for gmtime and as a
home for future generic time-related functions that are not in POSIX.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2019-07-17 14:04:44 +02:00
Peter A. Bigot
3e8df8b369 libc: minimal: provide gmtime implementation
Implement the conversion from UNIX time to broken-down civil time per
the gmtime() and gmtime_r() functions.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2019-07-17 14:04:44 +02:00
Peter A. Bigot
acc1703241 libc: minimal: provide types in time.h
Provide definitions for a subset of the standard time types that must be
provided by this file, in anticipation of supporting civil time in
Zephyr.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2019-07-17 14:04:44 +02:00
Nicolas Pitre
629bd85612 mempool: significant reduction of memory waste
The mempool allocator implementation recursively breaks a memory block
into 4 sub-blocks until it minimally fits the requested memory size.

The size of each sub-blocks is rounded up to the next word boundary to
preserve word alignment on the returned memory, and this is a problem.

Let's consider max_sz = 2072 and n_max = 1. That's our level 0.

At level 1, we get one level-0 block split in 4 sub-blocks whose size
is WB_UP(2072 / 4) = 520. However 4 * 520 = 2080 so we must discard the
4th sub-block since it doesn't fit inside our 2072-byte parent block.

We're down to 3 * 520 = 1560 bytes of usable memory.
Our memory usage efficiency is now 1560 / 2072 = 75%.

At level 2, we get 3 level-1 blocks, and each of them may be split
in 4 sub-blocks whose size is WB_UP(520 / 4) = 132. But 4 * 132 = 528
so the 4th sub-block has to be discarded again.

We're down to 9 * 132 = 1188 bytes of usable memory.
Our memory usage efficiency is now 1188 / 2072 = 57%.

At level 3, we get 9 level-2 blocks, each split into WB_UP(132 / 4)
= 36 bytes. Again 4 * 36 = 144 so the 4th sub-block is discarded.

We're down to 27 * 36 = 972 bytes of usable memory.
Our memory usage efficiency is now 972 / 2072 = 47%.

What should be done instead, is to round _down_ sub-block sizes
not _up_. This way, sub-blocks still align to word boundaries, and
they always fit within their parent block as the total size may
no longer exceed the initial size.

Using the same max_sz = 2072 would yield a memory usage efficiency of
99% at level 3, so let's demo a worst case 2044 instead.

Level 1: 4 sub-blocks of WB_DN(2044 / 4) = 508 bytes.
We're down to 4 * 508 = 2032 bytes of usable memory.
Our memory usage efficiency is now 2032 / 2044 = 99%.

Level 2: 4 * 4 sub-blocks of WB_DN(508 / 4) = 124 bytes.
We're down to 16 * 124 = 1984 bytes of usable memory.
Our memory usage efficiency is now 1984 / 2044 = 97%.

Level 3: 16 * 4 sub-blocks of WB_DN(124 / 4) = 28 bytes.
We're down to 64 * 28 = 1792 bytes of usable memory.
Our memory usage efficiency is now 1792 / 2044 = 88%.

Conclusion: if max_sz is a power of 2 then we get 100% efficiency at
all levens in both cases. But if not, then the rounding-up method has
a far worse degradation curve than the rounding-down method, wasting
more than 50% of memory in some cases.

So let's round sub-block sizes down rather than up, and remove
block_fits() which purpose was to identify sub-blocks that didn't
fit within their parent block and is now useless.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-16 14:21:21 -07:00
Nicolas Pitre
8e11970378 realloc(): struct sys_mem_pool_block is word aligned
Since commit 39cd2ebef7 ("malloc: make sure returned memory is
properly aligned") the size of struct sys_mem_pool_block size is
rounded up to the next word boundary.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-16 14:42:37 +02:00
Joakim Andersson
7a93e948a9 kernel: lib: Add convert functions for hex strings and binary arrays
Move duplicate hex2bin and add bin2hex function so that application can
use the functions and avoid code duplication.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-07-16 12:44:18 +02:00
Nicolas Pitre
3c0cc08657 prf.c: handle denormals properly
Denormals need to be normalized to be displayed properly.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-14 23:07:44 -04:00
Nicolas Pitre
53169743d2 prf.c: properly format INF/NAN/zero with prefix
The space or plus prefix must appear when requested even with INF and
NAN. And no zero-padding in that case.

Also, 0.0 and -0.0  are distinct values. It is necessary to display
the minus sign with a negative zero.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-14 23:07:44 -04:00
Nicolas Pitre
90ec5360be prf.c: fix wrong results with %g conversion
The precision parameter to the %g conversion indicates the maximum
number of significant digits and not the number of digits to appear
after the radix character. Here's a few examples this patch fixes:

                                expected        before
----------------------------------------------------------
printf("%.3g", 150.12)          150             150.12
printf("%.2g", 150.1)           1.5e+02         150.1
printf("%#.3g", 150.)           150.            150.000
printf("%#.2g", 15e-5)          0.00015         0.00
printf("%#.4g", 1505e-7)        0.0001505       0.0002
printf("%#.4g", 1505e-8)        1.505e-05       1.5050e-05

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-14 23:07:44 -04:00
Nicolas Pitre
132a286c39 prf.c: fix display of float exponent >= 100
The code accounts only for 2 exponent digits even though the exponent
may grow up to 308. Before this change, printf("%g", 1e300) would
produce "1e+N0".

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-14 23:07:44 -04:00
Nicolas Pitre
f286eda6f0 prf.c: remove arbitrary large stack buffer usage
The on-stack work buffer occupies 201 bytes by default. Now that we've
made the code able to cope with virtually unlimited width and precision
values, we can reduce stack usage to its strict minimum i.e. 25 bytes.

This allows for some additional sprintf tests exercizing wide results.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-14 23:07:44 -04:00
Nicolas Pitre
d28434b4fa prf.c: remove buffer limitation on precision and padding for floats
Even if the code used to limit the precision to the on-stack buffer
size, it was still possible to do:

    printf("%f", 1.0e300);

which would overflow the stack and crash the program. Let fix this issue
and remove the precision limitation by recording the number of zeroes to
insert while converting the value and generating those zeroes only
when outputting the data.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-14 23:07:44 -04:00
Nicolas Pitre
7c7f107885 prf.c: remove buffer limitation on field width and padding for integers
Zero-padding of integers took place in the on-stack buffer before
justification. Let's perform that padding on the fly while sending
out data instead.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-14 23:07:44 -04:00
Nicolas Pitre
33312cfd98 prf.c: remove buffer limitation on field width and string copy
The z_prf() function currently allocates a 200-byte buffer on the
stack to copy strings into, and then perform left/right alignment
and padding. Not only this is a pretty large chunk of stack usage,
but this imposes limitations on field width and string length. Also
the string is copied not only once but _thrice_ making this code
less than optimal.

Let's rework the code to get rid of both the field width limit and
string length limit, as well as the two extra memory copy instances.

While at it, let's fixes printf("%08s", "abcd") which used to
produce "0000abcd".

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-14 23:07:44 -04:00
Nicolas Pitre
4ad2a8f990 prf.c: don't be silent with unknown conversion specifiers
Mimic the glibc behavior when encountering an unknown conversion
specifier rather than silently skipping it.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-14 23:07:44 -04:00
Nicolas Pitre
ab59209ea6 prf.c: abstract output method call
This makes for nicer code by avoiding repetitions of the same pattern.
Changes to come will make more use of it.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-14 23:07:44 -04:00
Nicolas Pitre
e1d8c1f8ca prf.c: implement the "hh" length modifier
For completeness.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-14 23:07:44 -04:00
Nicolas Pitre
e9b1cc5f31 prf.c: code and style cleanup
Some cleanups before further changes:

- Remove dead leftover from the "case 's'" code.

- Remove needless parents and casts.

- Remove "register" qualifier as it is ignored. The compiler knows
  better these days.

- Adjust tabs assuming standard 8-columns tab spacing.

- Make multi-line comments start with "/*" on a line of its own.

- Make the format string const to match  prototypes in other files.

- Declare boolean variable and parameters as bool.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-14 23:07:44 -04:00
Nicolas Pitre
ff7e4e69c8 realloc(): fix possible memory leak
If size is equal to zero, and ptr is not NULL, then the call must be
equivalent to free(ptr).

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-12 14:08:02 -07:00
Nicolas Pitre
ffab197928 libc: fix memchr() prototype
The standard memchr() uses an int for its second argument.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-10 13:42:31 -07:00
Timo Teräs
55dc481a15 libc: add strnlen implementation
This is standard function and useful for application writers.

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
2019-07-10 13:41:20 -07:00
Jukka Rissanen
b889120ca7 Revert "libc: types: Remove wrong definition"
This reverts commit 2a63e342f4.

This needs to be reverted as otherwise the type of ssize_t will be
"unsigned long" which is not correct.

    (gdb) ptype ssize_t
    type = unsigned long

For example this check would fail in that case

    ssize_t foo(void)
    {
        return -1;
    }
    ...
    if (foo() < 0) {
        printk("This is never called\n");
    }

Fixes #17378

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2019-07-09 21:55:05 +03:00
Jan Van Winkel
18ef131c80 gui: Move lvgl into a zephyr module
Removed lvgl sources from CMakefile.txt and only keep zephyr glue
logic.

Further added lvgl module to west.yml.

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
2019-07-06 07:58:44 -04:00
Nicolas Pitre
1b359e6426 limits.h: streamline value definitions
Compilers (at least gcc and clang) already provide max value definitions
for basic types. It makes sense to rely on them to properly support
both 32-bit and 64-bit builds.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-05 10:05:37 -04:00
Nicolas Pitre
39cd2ebef7 malloc: make sure returned memory is properly aligned
The accounting data stored at the beginning of a memory block used by
malloc must push the returned memory address to a word boundary. This
is already the case on 32-bit systems, but not on 64-bit systems where
e.g. struct k_mem_block_id still has a size of 4.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-03 14:17:29 -07:00
Andy Ross
ee4c23cc1f CMSIS v2: Work around time unit confusion
The current CMSIS v2 implementation is clearly assuming that timeout
arguments being passed to e.g. osDelay() are in units of Zephyr ticks,
not milliseconds as specified by ARM or (inconsistently) assumed by
our test code.

Most tests work with the ~100 Hz default tick rate, but they tend to
fail on precision issues at higher tick rates.  Force the CMSIS v2
applications to be 1000 Hz for now as a workaround, and detect the
mismatch as a build failure.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-07-02 22:52:29 -04:00
Nicolas Pitre
fc4ca923bb mempool: fully use the inline free block bitmap on 64-bit targets
The "bits" field in struct sys_mem_pool_lvl is unioned with a pointer.
That leaves more space for inline free bits on 64-bit targets.
Let's declare it as an array and adjust its size based on the pointer
size. On 32-bit targets the generated code remains identical.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-02 19:41:20 -07:00
Nicolas Pitre
cf974371fb mempool: make alignment/rounding 64-bit compatible
Minimum alignment and rounding must be done on a word boundary. Let's
replace _ALIGN4() with WB_UP() which is equivalent on 32-bit targets,
and 64-bit aware.

Also enforce a minimal alignment on the memory pool. This is making
a difference mostly on64-bit targets where the widely used 4-byte
alignment is not sufficient.

The _ALIGN4() macro has no users left so it is removed.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-02 19:41:20 -07:00
Andrew Boie
d045bd7673 lib: os: exclude z_arch_printk_char_out()
This function doesn't do anything, and only exists so that
it can be overridden later, exclude from coverage reports.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-06-28 20:04:29 -07:00
Andrew Boie
05212e823f lib: os: fix vsnprintk coverage
vsnprintk() was uncovered. Simply adjust snprintk() to use
it, instead of duplicating logic.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-06-28 20:04:29 -07:00
Anas Nashif
5b0aa794b2 cleanup: include/: move misc/reboot.h to power/reboot.h
move misc/reboot.h to power/reboot.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
efb8df5366 cleanup: include/: move misc/stack.h to debug/stack.h
move misc/stack.h to debug/stack.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
a2fd7d70ec cleanup: include/: move misc/util.h to sys/util.h
move misc/util.h to sys/util.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
d222553931 cleanup: include/: move misc/speculation.h to sys/speculation.h
move misc/speculation.h to sys/speculation.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
536dd5a71f cleanup: include/: move misc/slist.h to sys/slist.h
move misc/slist.h to sys/slist.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
1859244b64 cleanup: include/: move misc/rb.h to sys/rb.h
move misc/rb.h to sys/rb.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
9ab2a56751 cleanup: include/: move misc/printk.h to sys/printk.h
move misc/printk.h to sys/printk.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
0c9e280547 cleanup: include/: move misc/mutex.h to sys/mutex.h
move misc/mutex.h to sys/mutex.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
08ee8b09ba cleanup: include/: move misc/mempool.h to sys/mempool.h
move misc/mempool.h to sys/mempool.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
1ed300b318 cleanup: include/: move misc/mempool_base.h to sys/mempool_base.h
move misc/mempool_base.h to sys/mempool_base.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
6ecadb03ab cleanup: include/: move misc/math_extras.h to sys/math_extras.h
move misc/math_extras.h to sys/math_extras.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
447311ec3e cleanup: include/: move misc/libc-hooks.h to sys/libc-hooks.h
move misc/libc-hooks.h to sys/libc-hooks.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
8be9f5de03 cleanup: include/: move misc/fdtable.h to sys/fdtable.h
move misc/fdtable.h to sys/fdtable.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
896b8d3c50 cleanup: include/: move misc/errno_private.h to sys/errno_private.h
move misc/errno_private.h to sys/errno_private.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
5eb90ec169 cleanup: include/: move misc/__assert.h to sys/__assert.h
move misc/__assert.h to sys/__assert.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
7435e5e089 cleanup: include/: move ring_buffer.h to sys/ring_buffer.h
move ring_buffer.h to sys/ring_buffer.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
9e35d53d1f cleanup: include/: move display.h to drivers/display.h
move display.h to drivers/display.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
f901e26de9 cleanup: include/: move hwinfo.h to drivers/hwinfo.h
move hwinfo.h to drivers/hwinfo.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
fe051a9055 cleanup: include/: move flash.h to drivers/flash.h
move flash.h to drivers/flash.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
0abdacf3a4 cleanup: include/: move json.h to data/json.h
move json.h to data/json.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
4e48e87fd2 cleanup: include/: move crc.h to sys/crc.h
move crc.h to sys/crc.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
bd977d06f8 cleanup: include/: move base64.h to sys/base64.h
move base64.h to sys/base64.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
e1e05a2eac cleanup: include/: move atomic.h to sys/atomic.h
move atomic.h to sys/atomic.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Anas Nashif
656f4dfdac cleanup: include/: move fs.h to fs/fs.h
move fs.h to fs/fs.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00
Daniel Leung
1c5fa6a128 cmake: use sdk-ng built toolchain for x86_64
This adds the necessary bits to utilize the x86_64 toolchain
built by sdk-ng for x86_64 when toolchain variant is either
zephyr or xtools. This allows decoupling the builds from
the host toolchain.

Newlib is also available with this toolchain so remove
the Kconfig restriction on CONFIG_NEWLIB_LIBC.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2019-06-27 16:08:32 -04:00
Flavio Ceolin
2a63e342f4 libc: types: Remove wrong definition
types.h was wrongly defining unsigned as signed and following
undefining it. This definition was not being used anywhere though.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2019-06-27 07:17:04 -04:00
Ioannis Glaropoulos
ff07fc7f0a lib: libc: fix alignment of HEAP base address for ARM
In ARM builds with support for user mode, i.e. with
CONFIG_USERSPACE=y, we need to align the beginning
of the heap space, to respect the ARM MPU region
alignment requirements.

Signed-off-by: Ioannis Glaropoulos <Ioannis.Glaropoulos@nordicsemi.no>
2019-06-26 09:11:45 -04:00
Ioannis Glaropoulos
53ec19c3cc lib: libc: newlib: define USED_RAM_END_ADDR only where it is needed
USED_RAM_END_ADDR is not used when Kconfig option
CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE is defined,
therefore, we do not need to define the macro in
that specific case.

Signed-off-by: Ioannis Glaropoulos <Ioannis.Glaropoulos@nordicsemi.no>
2019-06-26 09:11:45 -04:00
Nicolas Pitre
f32330b22c stdint.h: streamline type definitions
Compilers (at least gcc and clang) already provide definitions to
create standard types and their range. For example, __INT16_TYPE__ is
normally defined as a short to be used with the int16_t typedef, and
__INT16_MAX__ is defined as 32767. So it makes sense to rely on them
rather than hardcoding our own, especially for the fast types where
the compiler itself knows what basic type is best.

Using compiler provided definitions makes even more sense when dealing
with 64-bit targets where some types such as intptr_t and size_t must
have a different size and range. Those definitions are then adjusted
by the compiler directly.

However there are two cases for which we should override those
definitions:

* The __INT32_TYPE__ definition on 32-bit targets vary between an int
  and a long int depending on the architecture and configuration.
  Notably, all compilers shipped with the Zephyr SDK, except for the
  i586-zephyr-elfiamcu variant, define __INT32_TYPE__ to a long int.
  Whereas, all Linux configurations for gcc, both 32-bit and 64-bit,
  always define __INT32_TYPE__ as an int. Having variability here is
  not welcome as pointers to a long int and to an int are not deemed
  compatible by the compiler, and printing an int32_t defined with a
  long using %d makes the compiler to complain, even if they're the
  same size on 32-bit targets. Given that an int is always 32 bits
  on all targets we might care about, and given that Zephyr hardcoded
  int32_t to an int before, then we just redefine __INT32_TYPE__ and
  derrivatives to an int to keep the peace in the code.

* The confusion also exists with __INTPTR_TYPE__. Looking again at the
  Zephyr SDK, it is defined as an int, even even when __INT32_TYPE__ is
  initially a long int. One notable exception is i586-zephyr-elf where
  __INTPTR_TYPE__ is a long int even when using -m32. On 64-bit targets
  this is always a long int. So let's redefine __INTPTR_TYPE__ to always
  be a long int on Zephyr which simplifies the code, works for both
  32-bit and 64-bit targets, and mimics what the Linux kernel does.
  Only a few print format strings needed adjustment.

In those two cases, there is a safeguard to ensure the type we're
enforcing has the right size and fail the build otherwise.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-06-25 23:29:22 -04:00
Nicolas Pitre
d4b60d691c malloc: no longer need to round mempool's max_sz
Since commit 465b2cf31b this value is rounded at compile time instead.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-06-25 23:24:53 -04:00
Nicolas Pitre
1140bd090c mempool: properly use the inline free block bitmap
The free block bitmap uses either extra memory specified by a pointer
in struct sys_mem_pool_lvl or the space occupied by that pointer
directly if the bitmap length is small enough to fit it.

But the test is wrong. the inline bitmap should be used if the number
of required bits is smaller or _equal_ to the pointer size. Not doing so
would wrongly bounce the free block bitmap to extra memory when the
number of blocks is exactly 32, which is in disagreement with
Z_MPOOL_LBIT_WORDS() that correctly returns 0 in that case.

In theory that mean that this bug would causes an overflow of the free
block bitmap whenever one level has exactly 32 blocks. But right now
there is a separate bug fixed separately that over-sizes the extra block
bitmap mitigating this bug.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-06-25 23:24:05 -04:00
Andy Ross
d0490fe9f9 lib/os/mempool: Fix corruption case with block splitting
The block_fits() predicate was borked.  It would check that a block
fits within the bounds of the whole heap.  But that's not enough:
because of alignment changes between levels the sub-blocks may be
adjusted forward.  It needs to fit inside the PARENT block that it was
split from.

What could happen at runtime is that the last subblocks of a
misaligned parent block would overlap memory from subsequent blocks,
or even run off the end of the heap.  That's bad.

Change the API of block_fits() a little so it can extract the parent
region and do this properly.

Fixes #15279.  Passes test introduced in #16728 to demonstrate what
seems like the same issue.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-06-25 18:51:08 -07:00
Nicolas Pitre
465b2cf31b mempool: fix corruption of the free block bitmap and beyond
In z_sys_mem_pool_block_alloc() the size of the first level block
allocation is rounded up to the next 4-bite boundary. This means one
or more of the trailing blocks could overlap the free block bitmap.

Let's consider this code from kernel.h:

  #define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
       char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
                              + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \

The static pool allocation rounds up the product of maxsz and nmax not
size of individual blocks. If we have, say maxsz = 10 and nmax = 20,
the result of _ALIGN4(10 * 20) is 200. That's the offset at which the
free block bitmap will be located.

However, because z_sys_mem_pool_block_alloc() does this:

        lsizes[0] = _ALIGN4(p->max_sz);

Individual level 0 blocks will have a size of 12 not 10. That means
the 17th block will extend up to offset 204, 18th block up to 216, 19th
block to 228, and 20th block to 240. So 4 out of the 20 blocks are
overflowing the static pool area and 3 of them are even located
completely outside of it.

In this example, we have only 20 blocks that can't be split so there is
no extra free block bitmap allocation beyond the bitmap embedded in the
sys_mem_pool_lvl structure. This means that memory corruption will
happen in whatever data is located alongside the _mpool_buf_##name
array. But even with, say, 40 blocks, or larger blocks, the extra bitmap
size would be small compared to the extent of the overflow, and it would
get corrupted too of course.

And the data corruption will happen even without allocating any memory
since z_sys_mem_pool_base_init() stores free_list pointer nodes into
those blocks, which in turn may get corrupted if that other data is
later modified instead.

Fixing this issue is simple: rounding on the static pool allocation is
"misparenthesized". Let's turn

	_ALIGN4(maxsz * nmax)

into

	_ALIGN4(maxsz) * nmax

But that's not sufficient.

In z_sys_mem_pool_base_init() we have:

        size_t buflen = p->n_max * p->max_sz, sz = p->max_sz;
        u32_t *bits = (u32_t *)((u8_t *)p->buf + buflen);

Considering the same parameters as above, here we're locating the extra
free block bitmap at offset `buflen` which is 20 * 10 = 200, again below
the reach of the last 4 memory blocks. If the number of blocks gets past
the size of the embedded bitmap, it will overlap memory blocks.

Also, the block_ptr() call used here to initialize the free block linked
list uses unrounded p->max_sz, meaning that it is initially not locating
dlist nodes within the same block boundaries as what is expected from
z_sys_mem_pool_block_alloc(). This opens the possibility for allocated
adjacent blocks to overwrite dlist nodes, leading to random crashes in
the future.

So a complete fix must round up p->max_sz here too.

Given that runtime usage of max_sz should always be rounded up, it is
then preferable to round it up once at compile time instead and avoid
further mistakes of that sort. The existing _ALIGN4() usage on p->max_sz
at run time are then redundant.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-06-24 12:10:09 -07:00
Anas Nashif
f2cb20c772 docs: fix misspelling across the tree
Found a few annoying typos and figured I better run script and
fix anything it can find, here are the results...

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-19 15:34:13 -05:00
David B. Kinder
2aebc980e2 doc: fix Kconfig misspellings
Fix misspellings in Kconfig files missed during regular reviews.

Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2019-06-18 15:07:52 -04:00
Andrew Boie
db84a76379 lib: os: remove dead code
If multithreading is disabled, thread_entry() never runs
since we cannot create threads; the non-multithreading case
was simply dead code.

Indicate to code coverage that CODE_UNREACHABLE should be
skipped.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-06-18 09:08:01 -04:00
Nicolas Pitre
c47bf1fa5e prf.c: implement the ll length modifiers
This allows for printing long long values. Because the code size
increase may be significant, this is made optional on 32-bit targets.
On 64-bit targets this doesn't change the code much as longs and
long longs are the same size so it is always enabled in that case.

The test on MAXFLD has to be adjusted accordingly. Yet, its minimum
value wasn't large enough to store a full-scale octal value, so this
is fixed as well.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-06-17 10:58:09 -07:00
Nicolas Pitre
2b32059a61 printk: make it 64-bit compatible
On 64-bit systems the most notable difference is due to longs and
pointers being 64-bit wide. Therefore there must be a distinction
between ints and longs. Similar to the prf.c case, this patch properly
implements the h, hh, l, ll and z length modifiers as well as some small
cleanups.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-06-17 10:28:44 -07:00
Nicolas Pitre
a6891e0313 prf.c: make it 64-bit compatible
On 64-bit systems the most notable difference is due to longs and
pointers being 64-bit wide. Therefore there must be a distinction
between ints and longs.

This patch:

- Make support functions take a long rather than an int as this can
  carry both longs and ints just fine.

- Use unsigned values in _to_x() to cover the full unsigned range
  and avoid sign-extending big values. Negative values are already
  converted to unsigned after printing the minus sign. This also makes
  division and modulus operations slightly faster.

- Remove excessive casts around va_arg() and use proper types with it.

- Implement the l and z length modifiers as they're significant on
  64-bit targets. While at it, throw in the z modifier as well.
  Since they all come down to 32-bit values on 32-bit targets, the
  added code should get optimized away as duplicate by the compiler
  in that case.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-06-13 12:55:17 -07:00
Nicolas Pitre
03170c040c string.c: make it 64-bit compatible
Casting a pointer to an int produces warnings with 64-bit targets.
Furthermore, an int is not always the optimal memory element that
can be copied in that case.

Let's use uintptr_t to cast pointers to integers for alignment
determination purposes, and mem_word_t to denote the optimal memory
"word" that can be copied on the platform.

The mem_word_t definition is equivalent to uintptr_t by default.
However, some 32-bit targets such as ARM platforms with the LDRD/STRD
instructions could benefit from word_t being an uint64_t.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-06-07 13:19:51 -07:00
Nicolas Pitre
eeb2e67f2b time_t: define using a fixed-width type
Make sure type_t and suseconds_t keep the same size on 32-bit and
64-bit targets.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-06-06 14:22:09 -07:00
Anas Nashif
4c32258606 style: add braces around if/while statements
Per guidelines, all statements should have braces around them. We do not
have a CI check for this, so a few went in unnoticed.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-06 15:20:21 +02:00
Nicolas Pitre
4323d381e7 json: make it 64-bit compatible
The struct json_obj_descr definition allocates only 2 bits for type
alignment. Instead of using them literally minus 1 to encode 1, 2, or 4,
let's store the alignment's shift value instead so that 1, 2, 4 or 8 can
be encoded with the same 2 bits to accommodate 64-bit builds.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-06-05 07:47:41 -04:00
Christian Tavares
543de09357 lib: updatehub: add support ipv6
This extends the UpdateHub library code to allow the
use of IPV6 for communication.

Signed-off-by: Christian Tavares <christian.tavares@ossystems.com.br>
2019-06-05 00:20:37 +02:00
Christian Tavares
c5d74eccda lib: updatehub: add suport a CoAPS/DTLS
This extends the UpdateHub library code to allow the
use of CoAPS/DTLS for communication.

Refs: #13039.

Signed-off-by: Christian Tavares <christian.tavares@ossystems.com.br>
2019-06-05 00:20:37 +02:00
Christian Tavares
297ac3765f lib: updatehub: Add UpdateHub.io support
UpdateHub is an enterprise-grade solution which makes simple to
remotely update all your embedded devices in the field. It
handles all aspects related to sending Firmware Over-the-Air(FOTA)
updates with maximum security and efficiency, while you focus in
adding value to your product.

Signed-off-by: Christian Tavares <christian.tavares@ossystems.com.br>
Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
2019-06-05 00:20:37 +02:00
Jan Van Winkel
539d16fca8 gui: Corrected RGB888 pointer in LVGL write function
Corrected buffer pointer in LVGL zephyr_vdb_write function for
converting RGBA8888 to RGB888 pixel format.

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
2019-05-30 09:33:07 -04:00
Paul Sokolovsky
af529d1158 libc: minimal: Implement exit()/_exit() functions.
Behavior is similar to newlib version: print "exit" message and go
into infinite loop.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-05-23 09:27:59 -04:00
Ulf Magnusson
927012dd7f lvgl: kconfig: Remove menu and use a 'menuconfig' symbol
The 'Graphical user interface' menu currently contains just the
'LittlevGL Support' symbol and its indented children.

To remove one menu level, remove the 'Graphical user interface' menu,
rename the symbol to 'LittlevGL GUI library' (consistent with e.g.
'Logging' and 'Bluetooth'), and turn it into a 'menuconfig' symbol.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-05-21 14:19:20 -05:00
Alexander Mihajlovic
f19787bb84 posix: Fix calculation of clock base in clock_settime
Previous version calculated rt_clock_base incorrectly by subtracting
clock_gettime from the specified time. Effectively the following
formula was used.

    rt_clock_base := new_time - clock_gettime()

This is clearly incorrect when we consider what should happen if we
call clock_settime with the result of clock_gettime. It ought to be
approximately a no-op, but instead we end up zeroing the clock.

    rt_clock_base := clock_gettime() - clock_gettime() = 0

This patch fixes clock_settime by instead using k_uptime_get to
calculate rt_clock_base, like so:

    rt_clock_base := new_time - k_uptime_get()

Trying the earlier thought experiment we get:

    rt_clock_base := clock_gettime() - k_uptime_get()

Using the definition of clock_gettime this expands to:

    rt_clock_base := (rt_clock_base + k_uptime_get()) - k_uptime_get()

The two k_uptime_get() terms cancel out, leaving:

    rt_clock_base := rt_clock_base

I.e. the no-op that we expect when calling clock_settime with
the result of clock_gettime.

Note: The bug is only observable when rt_clock_base is non-zero.
So when clock_settime is called for the first time, it will appear
to work correctly since rt_clock_base is initialized to 0.

Signed-off-by: Alexander Mihajlovic <alexander.mihajlovic@endian.se>
2019-05-21 08:24:59 -04:00
Benoit Leforestier
472ea92e79 Build: Build with newlib-nano c library
Add an option for building with newlib-nano library.
The newlib-nano library for ARM embedded processors is a part of the
GNU Tools for ARM Embedded Processors.
Add mem_alloc tests with newlib nano.

Signed-off-by: Benoit Leforestier <benoit.leforestier@gmail.com>
2019-05-15 10:46:44 -05:00
Jakob Olesen
c8708d9bf3 misc: Replace uses of __builtin_*_overflow() with <misc/math_extras.h>.
Use the new math_extras functions instead of calling builtins directly.

Change a few local variables to size_t after checking that all uses of
the variable actually expects a size_t.

Signed-off-by: Jakob Olesen <jolesen@fb.com>
2019-05-14 19:53:30 -05:00
Wentong Wu
6922d93d8e lib/posix: correct the meaning of CONFIG_MAX_PTHREAD_COUNT
Current code implement CONFIG_MAX_PTHREAD_COUNT as the maximum number
of POSIX threads that can ever be created, rather than the maximum
number of active POSIX threads. Use pthread_state of struct posix_thread
to track the state of posix thread in posix_thread_pool so that we can
reuse the unused posix thread.

Fixes #15516.

Signed-off-by: Wentong Wu <wentong.wu@intel.com>
2019-05-11 08:24:36 -04:00
Paul Sokolovsky
81fa697f32 posix: Move gettimeofday declaration to <sys/time.h>
According to POSIX, that's the header which defines this function.
Similarly, nothing in POSIX indicates that <time.h> should have
access to struct timeval, so it's removed (it's made accessible
to <sys/time.h> via <sys/_timeval.h> introduced earlier).

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-05-07 22:18:09 -04:00
Paul Sokolovsky
c896f87d5d libc: minimal: Introduce sys/_timeval.h
This is implementation-level header which defines struct timeval, and
intended to be included by headers which need this structure. This
implementation scheme is compatible with Newlib, and thus provides a
step to use minlibc vs Newlib interchangeably.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-05-07 22:18:09 -04:00
Paul Sokolovsky
d79d3eb725 libc: minimal: Define time_t and suseconds_t
According to POSIX, these types should be defined by sys/types.h.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-05-07 22:18:09 -04:00
Andrew Boie
7153a2eec5 lib: cmsis_v1: set stack size larger if coverage
Use some large value, coverage targets have lots of RAM.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-05-02 15:42:33 -04:00
Andrew Boie
8502a2ea73 lib: cmsis_v2: increase stack sizes if coverage
Set to 4096, coverage targets have lots of RAM.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-05-02 15:42:33 -04:00
Andrew Boie
16317c1a9f lib: cmsis_v2: set default dynamic stack size
Set to same default as regular stacks. This doesn't use
any extra memory until CMSIS_V2_THREAD_DYNAMIC_MAX_COUNT
is set. 0 is not a valid default if that is set.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-05-02 15:42:33 -04:00
Andrew Boie
b8c560a439 newlib: fix user mode calls to _sbrk
Depending on configuration, this value could end up as
a variable and not an array symbol, causing a crash if
newlib decides to call _sbrk on behalf of a user thread,
which needs to perform arithmetic on it.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-05-02 07:25:19 -04:00
Balaji Kulkarni
a25dce964b libc: minimal: Add bsearch function
This function implements generic binary-search.

Fixes #15159

Signed-off-by: Balaji Kulkarni <balaji.kulkarni92@gmail.com>
2019-04-25 20:39:36 -07:00
Tomasz Gorochowik
1afa9d0e5d libc: minimal: fix realloc function
Excerpt from the manual:

  If ptr is NULL, then the call is equivalent to malloc(size) [...]

Without this commit, such calls end with a BUS FAULT.

Signed-off-by: Tomasz Gorochowik <tgorochowik@antmicro.com>
2019-04-19 16:17:14 -04:00
Anas Nashif
3ae52624ff license: cleanup: add SPDX Apache-2.0 license identifier
Update the files which contain no license information with the
'Apache-2.0' SPDX license identifier.  Many source files in the tree are
missing licensing information, which makes it harder for compliance
tools to determine the correct license.

By default all files without license information are under the default
license of Zephyr, which is Apache version 2.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-04-07 08:45:22 -04:00
Patrik Flykt
4aa48833d8 subsystems: Rename reserved function names
Rename reserved function names in the subsys/ subdirectory except
for static _mod_pub_set and _mod_unbind functions in bluetooth mesh
cfg_srv.c which clash with the similarly named global functions.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2019-04-03 17:31:00 -04:00
Andrew Boie
c8aee7b413 sys_mem_pool: use sys_mutex
Permission management no longer necessary, the former
parameter for the mutex is now simply ignored.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-04-03 13:47:45 -04:00
Andrew Boie
f0835674a3 lib: os: add sys_mutex data type
For systems without userspace enabled, these work the same
as a k_mutex.

For systems with userspace, the sys_mutex may exist in user
memory. It is still tracked as a kernel object, but has an
underlying k_mutex that is looked up in the kernel object
table.

Future enhancements will optimize sys_mutex to not require
syscalls for uncontended sys_mutexes, using atomic ops
instead.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-04-03 13:47:45 -04:00
Pawel Dunaj
2189d9b56d lib: mempool: Alloc and break must happen atomically
This fixes a regression caused by 41e90630d.

Signed-off-by: Pawel Dunaj <pawel.dunaj@nordicsemi.no>
2019-04-03 12:36:36 -04:00
Patrik Flykt
21358baa72 all: Update unsigend 'U' suffix due to multiplication
As the multiplication rule is updated, new unsigned suffixes
are added in the code.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2019-03-28 17:15:58 -05:00
Patrik Flykt
24d71431e9 all: Add 'U' suffix when using unsigned variables
Add a 'U' suffix to values when computing and comparing against
unsigned variables.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2019-03-28 17:15:58 -05:00
Flavio Ceolin
c2b25151cb lib: printk: Make if/iterations evaluate boolean operands
MISRA-C rule 14.4

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2019-03-26 22:06:45 -04:00
Flavio Ceolin
44fc55e209 lib: crc16_sw: Add missing U to unsigned constants
Add U to unsigned integer constants to avoid implicit cast.

MISRA-C rule 10.1

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2019-03-26 22:06:45 -04:00
Flavio Ceolin
ce696e9aa2 lib: rb: Make operands have an appropriate essential type
MISRA-C 8.10.2 defines essential operand types and how to handle them
through rules 10.1 .. 10.5. This commit adds an U to unsigned constants
to avoid implicit casts and make if/while statements evaluate a boolean
to avoid other types being casted to boolean.

MISRA-C rules 10.1, 10.2 and 10.3

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2019-03-26 22:06:45 -04:00
Flavio Ceolin
9d5936c04f lib: posix: fs: Fix access invalid memory
fs_dirent.name is MAX_FILE_NAME + 1 bytes long, not PATH_MAX. Just
fixing it to avoid access invalid memory.

Coverity CID: 186037

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2019-03-23 09:52:51 -05:00
Vincent Wan
3609e261bb net: sockets: move fcntl back to socket_offload.c
We are reverting the changes in commit
55b3f05932 given build errors are seen
when fcntl.h is included, as it declares fcntl() as a non-static
function. The same function cannot be declared as both static and
non-static.

Instead, we avoid redefining fcntl() in lib/os/fdtable.c specifically
for case of the SimpleLink family, til we have support for the new
socket_op_vtable.

Signed-off-by: Vincent Wan <vincent.wan@linaro.org>
2019-03-20 11:36:18 -05:00
Pawel Dunaj
41e90630d7 lib: mempool: Synchronize level checks
Do not perform early level usage check. This can lead to situation
where block is seen as available on level when it was taken from
the other context.

Fixes: #14504

Signed-off-by: Pawel Dunaj <pawel.dunaj@nordicsemi.no>
2019-03-19 13:38:46 -05:00
Pawel Dunaj
2c7d68009a lib: mempool: Return error if no block found
Return -ENOMEM if no block is available on any level.

Fixes: #14504

Signed-off-by: Pawel Dunaj <pawel.dunaj@nordicsemi.no>
2019-03-19 13:38:46 -05:00
Krzysztof Chruscinski
9502b8b80b lib: os: ring_buffer: Fix not handled return value
Some function return values were not handled. Added assert in case
those functions return error. It is possible only if same ring buffer
instance is used without any protection from multiple contexts.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2019-03-14 08:40:18 +01:00
Ulf Magnusson
53376394b7 kconfig: Remove blank lines at the beginning/end of files
Maybe this is some "just in case" thing that got copied around. There's
no need to have a blank line at the beginning or end of Kconfig files.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-03-13 07:29:42 -05:00
Kumar Gala
7aa8e43add libc: newlib: Cleanup/fix setting of system include paths
When we build with newlib we don't set -nostdinc.  In that case make
sure that we leave it to the toolchain to set the system include paths.

The one exception to leaving to the toolchain to set the system include
paths is the path to the newlib headers.  Since we build
with -ffreestanding we need to make sure the newlib header path is the
before the toolchain headers. Otherwise the toolchain's 'freestanding'
headers get picked up and that causes issues (for example getting PRI*64
defined properly from inttypes.h due to __STDC_HOSTED__ being '0').

For newlib we accomplish this by having the only system header specified
by zephyr_system_include_directories() being just the newlib headers.

Note: for minlibc we leave things alone as things just happen to work as
the -I include of the libc headers takes precedence over -isystem so we
get the libc headers over the toolchain ones.  For the newlib case it
appears that setting both -I and -isystem for the same dir causes the
-I to be ignored.

Fixes #14310

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2019-03-13 06:50:23 -05:00
Maureen Helm
f8c4808d96 boards: mimxrt10{50,60}_evk: Set lvgl defaults in board defconfigs
Configure lvgl defaults for imx rt boards in their respective board
defconfigs rather than the lvgl sample application.

Signed-off-by: Maureen Helm <maureen.helm@nxp.com>
2019-03-13 06:02:42 -05:00
Kumar Gala
276f766317 libc: rename _zephyr_fputc to zephyr_fputc
For some reason we missed _zephyr_fputc in commit
4344e27c26.  Rename _zephyr_fputc to just
zephyr_fputc and fixup associated code to build.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2019-03-12 13:59:06 -05:00
Kumar Gala
c82f23cada libc: Fix fwrite function name
Commit 4344e27c26 changed the reserved
function names, but got the naming wrong for fwrite.  Just use the
name zephyr_fwrite everywhere.

Fixes #14275

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2019-03-12 13:59:06 -05:00
Patrik Flykt
4344e27c26 all: Update reserved function names
Update reserved function names starting with one underscore, replacing
them as follows:
   '_k_' with 'z_'
   '_K_' with 'Z_'
   '_handler_' with 'z_handl_'
   '_Cstart' with 'z_cstart'
   '_Swap' with 'z_swap'

This renaming is done on both global and those static function names
in kernel/include and include/. Other static function names in kernel/
are renamed by removing the leading underscore. Other function names
not starting with any prefix listed above are renamed starting with
a 'z_' or 'Z_' prefix.

Function names starting with two or three leading underscores are not
automatcally renamed since these names will collide with the variants
with two or three leading underscores.

Various generator scripts have also been updated as well as perf,
linker and usb files. These are
   drivers/serial/uart_handlers.c
   include/linker/kobject-text.ld
   kernel/include/syscall_handler.h
   scripts/gen_kobject_list.py
   scripts/gen_syscall_header.py

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2019-03-11 13:48:42 -04:00
Andrew Boie
7416457b87 lib: fdtable: safely bounds check file descriptors
Prevent speculative attacks with out-of-bounds fd
values.

Won't affect code generation for systems that don't
enable CONFIG_BOUNDS_CHECK_BYPASS_MITIGATION.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-03-11 09:54:04 -07:00
Dennis Wildmark
ce4b282717 lib: cmsis_rtos_v2: changed printformat that produced warnings
Changed the print format for unsigned 32-bit variables that produced
warnings when compiled with newlib instead of the standard C library.
Chose to replace %d with PRIu32 because the latter is more portable
and adapts to the types of the standard C libraries.
Tested with and without newlib, and with sanitycheck.

Signed-off-by: Dennis Wildmark <dennis.wildmark@assaabloy.com>
2019-03-11 11:02:20 -05:00
Ulf Magnusson
da704c453f kconfig: lvgl: Remove redundant LVGL dep.
Appears within an 'if LVGL'.

'if FOO' is just shorthand for adding 'depends on FOO' to each item
within the 'if'. Dependencies on menus work similarly. There are no
"conditional includes" in Kconfig, so 'if FOO' has no special meaning
around a source. Conditional includes wouldn't be possible, because an
if condition could include (directly or indirectly) forward references
to symbols not defined yet.

Tip: When adding a symbol, check its dependencies in the menuconfig
('ninja menuconfig', then / to jump to the symbol). The menuconfig also
shows how the file with the symbol got included, so if you see
duplicated dependencies, it's easy to hunt down where they come from.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-03-09 09:48:25 -05:00
Filip Brozovic
fb3d9744cc lvgl: lvgl_color_1: add support for horizontally tiled displays
Enable displays which do not have SCREEN_INFO_MONO_VTILED set to work
with LittlevGL

Signed-off-by: Filip Brozovic <fbrozovic@gmail.com>
2019-03-07 07:18:50 -05:00
Patrik Flykt
e8f6ea2c8c lib/os: Remove case ranges from printk
Remove case ranges from printk in order to clean up GNUisms
and make the code standards compliant.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2019-03-06 17:44:04 -05:00
Anas Nashif
3caea8c81e libc: minimal: add prototype of rand()
Add prototype of rand() that can be defined in tests.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-03-04 17:32:08 -08:00
Andrew Boie
7707060959 userspace: get rid of app section placeholders
We used to leave byte-long placeholder symbols to ensure
that empty application memory sections did not cause
build errors that were very difficult to understand.

Now we use some relatively portable inline assembly to
generate a symbol, but don't take up any extra space.

The malloc and libc partitions are now only instantiated
if there is some data to put in them.

Fixes: #13923

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-03-04 08:05:16 -08:00
Tim Sørensen (TIMS)
630af8a65f misc: assert_post_action: Added __weak keyword.
Added __weak keyword to to support overriding assert_post_action().
This allows system designers to change/augment the assert behaviour,
i.e. add logging to persistant storage of program counter, line
number etc, and/or change reboot behaviour.

Signed-off-by: Tim Sørensen (TIMS) <tims@oticon.com>
2019-03-02 12:26:21 -05:00
Charles E. Youse
3522e05f76 posix/pthreads: fix pthread_barrier_wait() behavior to match Posix
Exactly one caller of pthread_barrier_wait() should receive a return
value of PTHREAD_BARRIER_SERIAL_WAIT; all others should receive zero
(or an error code). Added a test to match.

Fixes: #9953

Signed-off-by: Charles E. Youse <charles.youse@intel.com>
2019-03-02 03:40:06 +01:00
Flavio Ceolin
1556fce845 libc: ctype: Fix operations between signed and unsigned types
MISRA-C says that char type should not be used in arithmetically as the
data doesn't represent numbers.

MISRA-C rules 10.1 and 10.2

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2019-03-01 09:22:24 +01:00
Andy Ross
85d895c60e lib/os: Remove recursion from mempool and rbtree
MISRA rules (see #11425) forbid recursive algorithms.  In the case of
rb_walk(), it's not actually used anywhere but a test right now, so we
can simply disable the API when CONFIG_MISRA_SANE is defined.  Mempool
had a (IMHO, fairly clever) tail recursive loop in bfree_recombine()
which can be trivially transformed into an only slightly uglier
iterative version.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-02-28 10:06:35 -08:00
Andy Ross
fe04adf99b lib/os: Conditionally eliminate alloca/VLA usage
MISRA rules (see #9892) forbid alloca() and family, even though those
features can be valuable performance and memory size optimizations
useful to Zephyr.

Introduce a MISRA_SANE kconfig, which when true enables a gcc error
condition whenever a variable length array is used.

When enabled, the mempool code will use a theoretical-maximum array
size on the stack instead of one tailored to the current pool
configuration.

The rbtree code will do similarly, but because the theoretical maximum
is quite a bit larger (236 bytes on 32 bit platforms) the array is
placed into struct rbtree instead so it can live in static data (and
also so I don't have to go and retune all the test stack sizes!).
Current code only uses at most two of these (one in the scheduler when
SCHED_SCALABLE is selected, and one for dynamic kernel objects when
USERSPACE and DYNAMIC_OBJECTS are set).

This tunable is false by default, but is selected in a single test (a
subcase of tests/kernel/common) for coverage.  Note that the I2C and
SPI subsystems contain uncorrected VLAs, so a few platforms need to be
blacklisted with a filter.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-02-28 10:06:35 -08:00
Paul Sokolovsky
d01f75be7e lib: os: fdtable: Add underscore aliases for read/write/close/lseek
These get references by newlib builds in other toolchains, e.g.
gnuarmemb, and lack of them breaks linking. Tested that
tests/posix/fs and tests/posix/common actually work with these
changes.

Fixes: #13906

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-02-28 08:45:13 -06:00
Ulf Magnusson
9aab5cef96 kconfig: Remove redundant 'default n' properties
Some more were added since the cleanup pass in June 2018. See e.g.
commit 2d50da70a1 ("drivers: ipm: Kconfig: Remove redundant 'default n'
properties") for a motivation. It also avoids people wondering whether
or not they need to put in 'default n'.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-02-27 09:25:22 +01:00
Rajavardhan Gundi
60186eba8c lib: cmsis_rtos_v2: kernel: Fix a coverity issue
Fixes an issue corresponding to CID: 190641

Fixes #12292.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2019-02-27 09:23:56 +01:00
Paul Sokolovsky
4bcd560985 lib: fdtable: Update for zephyr_write() rename
_impl__zephyr_write() was renamed to _impl__zephyr_write_stdout().
This wasn't caught by CI because we didn't have POSIX tests build
for newlib, but now we have.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-02-26 14:56:58 -06:00
Paul Sokolovsky
b5639c4ee2 libc: newlib: Rename adhoc read/write implementation for stdin/stdout
In case newlib is enabled, but POSIX subsys isn't, there're adhoc
implementations of read() and write() which work only with adhoc
stdin/stdout emulation layer. These are backed by system calls named
like "read" and "write". Rename all these functions and syscalls to
explicitly mention stdin/stdout in the names, to free namespace
for the implementation of generic read/write syscalls which will
integrate with POSIX fdtable.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2019-02-26 11:36:33 -06:00
Andrew Boie
feab37096b libc: fix CONFIG_STDOUT_CONSOLE semantics
The intent of this Kconfig is to allow libc stdout
functions like printf() to send their output to the
active console driver instead of discarding it.

This somehow evolved into preferring to use
printf() instead of printk() for all test case output
if enabled. Libc printf() implementation for both
minimal libc and newlib use considerably more stack
space than printk(), with nothing gained by using
them.

Remove all instances where we are conditionally
sending test case output based on this config, enable
it by default, and adjust a few tests that disabled
this because they were blowing stack.

printk() and vprintk() now work as expected for
unit_testing targets, they are just wrappers for
host printf().

Fixes: #13701

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-02-26 08:00:33 -06:00
Anas Nashif
59841c9c67 libc: do not enable newlib on x86_64
Also, for now x86_64 does not support newlib, so do not enable newlib
for this arch until we have a solution.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-02-24 17:48:10 -05:00
Kumar Gala
6cc5722a61 lib: posix: Fix compile error with mqueue.h
With newer newlib we get a build error with mqueue.h realted to mode_t.
Let's just let newlib define mode_t and have minimal libc also define
it in sys/types.h.  So we remove the duplicated definition in
posix/unistd.h.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2019-02-23 11:42:31 -06:00
Andrew Boie
4ce652e4b2 userspace: remove APP_SHARED_MEM Kconfig
This is an integral part of userspace and cannot be used
on its own. Fold into the main userspace configuration.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-02-23 07:43:55 -05:00
Andrew Boie
17ce822ed9 app_shmem: create generic libc partition
We need a generic name for the partition containing
essential C library globals. We're going to need to
add the stack canary guard to this area so user mode
can read it.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-02-22 18:50:43 -05:00
Kumar Gala
e96d02984c lib: posix: Fix compile issue with newer newlib
Both SDK 0.10.0-beta2 and the ARM gcc 2018q2 run into a build issue with
newlib and conflict definitions of mode_t type.

First we need to add some ifdef protection if mode_t is already defined
and set _MODE_T_DECLARED if we are the first to define it.

Secondarily, we rename include/posix/sys/types.h to
include/posix/posix_types.h so that we aren't getting a name collusion
with the system sys/types.h and that we can easily and clearily include
it (which we need to do to pull in the info from newlib).

Fixes: #12224

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2019-02-22 11:55:01 -06:00
Kumar Gala
b730fa4ccb posix_lib: Disable support on native posix arch
For now we are disabling support for POSIX lib on native posix arch.  We
need to cleanup and support POSIX lib cleanly for hardware targets.
Once that is working properly we can look to support the feature on
native posix arch.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2019-02-22 11:55:01 -06:00
Anas Nashif
04743c9a79 posix: rename priority in sched_param struct
Priority member in the sched_param struct should be named
sched_priority.

Fixes #13470

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-02-19 11:24:45 -05:00
Carlos Stuart
75f77db432 include: misc: util.h: Rename min/max to MIN/MAX
There are issues using lowercase min and max macros when compiling a C++
application with a third-party toolchain such as GNU ARM Embedded when
using some STL headers i.e. <chrono>.

This is because there are actual C++ functions called min and max
defined in some of the STL headers and these macros interfere with them.
By changing the macros to UPPERCASE, which is consistent with almost all
other pre-processor macros this naming conflict is avoided.

All files that use these macros have been updated.

Signed-off-by: Carlos Stuart <carlosstuart1970@gmail.com>
2019-02-14 22:16:03 -05:00
Andy Ross
ec554f44d9 kernel: Split reschdule & pend into irq/spin lock versions
Just like with _Swap(), we need two variants of these utilities which
can atomically release a lock and context switch.  The naming shifts
(for byte count reasons) to _reschedule/_pend_curr, and both have an
_irqlock variant which takes the traditional locking.

Just refactoring.  No logic changes.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-02-08 14:49:39 -05:00
Carlos Stuart
f5f450eeee lib: cmsis_rtos_v2: Join and detach support
Implements osThreadJoin and osThreadDetach.

This implementation uses a semaphore to signal when a thread is
exiting so any join operations are signalled to continue. It supports
multiple join operations on a single thread, and ensures joins are
aborted if a thread is detached.

Signed-off-by: Carlos Stuart <carlosstuart1970@gmail.com>
2019-02-08 11:59:38 -05:00
Jan Van Winkel
d5b71ca756 gui: Corrected typo in object realign Kconfig option
Corrected typo in lv_conf.h for object realign Kconfig option.

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
2019-02-08 09:35:18 -06:00
Andrew Boie
41f6011c36 userspace: remove APPLICATION_MEMORY feature
This was never a long-term solution, more of a gross hack
to get test cases working until we could figure out a good
end-to-end solution for memory domains that generated
appropriate linker sections. Now that we have this with
the app shared memory feature, and have converted all tests
to remove it, delete this feature.

To date all userspace APIs have been tagged as 'experimental'
which sidesteps deprecation policies.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-02-08 07:04:30 -05:00
Andrew Boie
4b4f773484 libc: set up memory partitions
* Newlib now defines a special z_newlib_partition containing
  all globals relevant to newlib. Most of these are in libc.a
  with a heap tracking variable in newlib's hooks.

* Both C libraries now expose a k_mem_partition containing the
  bounds of the malloc heap arena. Threads that want to use
  libc malloc() will need to add this to their memory domain.

* z_newlib_get_heap_bounds has been removed, in favor of the
  memory partition for the heap arena

* ztest now includes the C library partitions in its memory
  domain.

* The mem_alloc test now runs in user mode to prove that this
  all works for both C libraries.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-02-08 07:04:30 -05:00
Carlos Stuart
655d3cc2b0 lib: cmsis_rtos_v2: Default thread prioity
If an unitialized/zeroed optional attribute was passed to osThreadNew
the priority would be osThreadNone i.e. uninitialized. This causes an
ASSERT to be hit as the priority isn't valid (it is not between
osPriorityIdle and osPriorityISR).

The fix checks the passed in priority is not osPriorityNone and assigns
osPriorityNormal. This is the correct CMSIS behaviour.

The ASSERT will still be hit if the priority is invalid (<0).

Signed-off-by: Carlos Stuart <carlosstuart1970@gmail.com>
2019-02-06 10:20:17 -05:00
Carlos Stuart
d47178bc95 lib: cmsis_rtos_v2: Default name if name is NULL
Fixed an issue whereby if an attribute structure was passed into a CMSIS
RTOS v2 'new' function with an invalid address i.e. NULL assigned to the
name (char*) member the memcpy at the end of each new function
would cause a segmentation fault i.e. read from an invalid
address.

This has been fixed by checking if the name is NULL and using the
default name from the init struct if it is. This is the same name
that would be used if not passing in the optional attr function
argument.

Changed the memcpy to strncpy to ensure that the copy does not read
beyond the end of the source string and changed the length from 16 to 15
(by means of a `sizeof(...)-1`) of the destination buffer to ensure that
it will always be nul-terminated.

Signed-off-by: Carlos Stuart <carlosstuart1970@gmail.com>
2019-02-06 10:20:17 -05:00
Carlos Stuart
d4eb2c9014 lib: cmsis_rtos_v2: Dynamic thread stacks
Implemented dynamic thread stacks for CMSIS threads by declaring an
array of default sized thread stacks. Allocation cannot be done on the
heap as some architectures require strict alignment for stacks so the
macro must be used to define the stack to ensure most compatibility.

Added a Kconfig variable to limit the number of dynamic threads on the
system (they also count towards total CMSIS thread count). This is so a
developer can have fine grained control over how many dynamic threads
can be allocated because all their stacks must be allocated up front so
could use a lot of memory needlessly if oversubscribed. The default
value is 0 which effectively disabled dynamic threads but also reduces
the memory impact to almost none.

Fixed an assert bug where thread_num was being tested against the
maximum allowed CMSIS threads - it previous checked for less than or
equal which actually (due to when the increment happens) allowed there
to be one more thread. The check now correctly uses less than and only
allowed up to the defined maximum.

Signed-off-by: Carlos Stuart <carlosstuart1970@gmail.com>
2019-02-06 10:20:17 -05:00
Carlos Stuart
48320433e9 lib: cmsis_rtos_v2: Dynamic memory pools
Implemented dynamic allocation of memory pools in a similar to manner to
what was already implemented for message queues. Added all the same
checks on size vs. maximum allowed and current heap.

Added an additional Kconfig variable to define the maximum size of a
dynamically allocated memory pool.

Signed-off-by: Carlos Stuart <carlosstuart1970@gmail.com>
2019-02-06 10:20:17 -05:00
Carlos Stuart
17db516069 lib: cmsis_rtos_v2: Message queue checks
Added some additional checks when creating a message queue to ensure the
size of the queue does not exceed the size of the buffer passed in via
the optional attributes.

Added a new Kconfig option to limit the maximum size of a message queue
dynamically allocated on the heap.

Added a check to ensure the heap is at least large enough to hold a
maximum size dynamically allocated queue.

Signed-off-by: Carlos Stuart <carlosstuart1970@gmail.com>
2019-02-06 10:20:17 -05:00
Carlos Stuart
07a1a60df2 lib: cmsis_rtos_v2: Additional Kconfig dependency
Added Kconfig dependency that NUM_PREEMPT_PRIORITIES must be at least
osPriorityISR (56). This was enforced by a build assert message but not
decribed in the Kconfig.

Signed-off-by: Carlos Stuart <carlosstuart1970@gmail.com>
2019-02-06 10:20:17 -05:00
Carlos Stuart
ccf51e2f50 lib: cmsis_rtos_v2: Uncrustify source files
Ran uncrustify on all library source files to ensure a compliant base to
work from.

Signed-off-by: Carlos Stuart <carlosstuart1970@gmail.com>
2019-02-06 10:20:17 -05:00
Jan Van Winkel
edfd41f6de gui: Upstep LittlevGL to version 5.3
Upstep LittlevGL to version 5.3

Signed-off-by: Jan Van Winkel <vanwinkeljan@gmail.com>
2019-02-04 15:32:29 -05:00
Alberto Escolar Piedras
c2699dd52d misc: assert_post_action: Treat POSIX arch like others
After #12732, 6904501173
asserts call k_panic.

Before this, the POSIX arch had its own hack in the
__ASSERT_POST implementation to terminate the process instead
of spining forever.

But the POSIX arch does implement k_panic properly, so there
is no need anymore for this hack.
=> Remove the special treatment for POSIX ARCH

Signed-off-by: Alberto Escolar Piedras <alpi@oticon.com>
2019-02-03 10:28:51 -08:00
Krzysztof Chruscinski
6904501173 misc: Add k_panic on assert
Replaced forever loop in assert with call to a function.
In post_assert_action() function, k_panic is called.

Forever loop was preventing logs to be printed and had behavior
ependent on the context (low prioriy thread - system continue to
ork, irq - system is blocked).

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2019-02-02 15:58:33 -08:00
Dominik Rekawek
499feaf50e lib: posix: fix build break due to size comparsion
In limits.h PATH_MAX is defined to same value as in nffs

Signed-off-by: Dominik Rekawek <dominik@itetech.pl>
2019-02-01 23:41:09 -05:00
Kumar Gala
f75ddb8dc7 gui: Fix typo in Kconfig option
CONFIG_ isn't used inside Kconfig files.  Remove use.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2019-02-01 19:01:16 -05:00
Anas Nashif
b832a1e264 printk: move into lib/os
Last file under misc/. Put it alongside other support functions in
lib/os.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-01-22 07:45:22 -05:00
Anas Nashif
db92e5c66e lib: flatten all loose components into one lib
lib/ was starting to get messy and inconsitent. Files being either
dumped in the root or in sub-directories without a clear plan.
Move all library components into one single folder and call it 'os'.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-01-22 07:45:22 -05:00
Niranjhana N
81ec05548d lib: posix: fix a wrong type in mqueue.c
mq_maxmsg and mq_msgsize are defined to be of
type long in POSIX standard. So use long for
variables that hold its value in mq_open().

Signed-off-by: Niranjhana N <niranjhana.n@intel.com>
2019-01-15 17:48:34 -05:00
Andy Ross
b69d0da82d arch/x86_64: New architecture added
This patch adds a x86_64 architecture and qemu_x86_64 board to Zephyr.
Only the basic architecture support needed to run 64 bit code is
added; no drivers are added, though a low-level console exists and is
wired to printk().

The support is built on top of a "X86 underkernel" layer, which can be
built in isolation as a unit test on a Linux host.

Limitations:

+ Right now the SDK lacks an x86_64 toolchain.  The build will fall
  back to a host toolchain if it finds no cross compiler defined,
  which is tested to work on gcc 8.2.1 right now.

+ No x87/SSE/AVX usage is allowed.  This is a stronger limitation than
  other architectures where the instructions work from one thread even
  if the context switch code doesn't support it.  We are passing
  -no-sse to prevent gcc from automatically generating SSE
  instructions for non-floating-point purposes, which has the side
  effect of changing the ABI.  Future work to handle the FPU registers
  will need to be combined with an "application" ABI distinct from the
  kernel one (or just to require USERSPACE).

+ Paging is enabled (it has to be in long mode), but is a 1:1 mapping
  of all memory.  No MMU/USERSPACE support yet.

+ We are building with -mno-red-zone for stack size reasons, but this
  is a valuable optimization.  Enabling it requires automatic stack
  switching, which requires a TSS, which means it has to happen after
  MMU support.

+ The OS runs in 64 bit mode, but for compatibility reasons is
  compiled to the 32 bit "X32" ABI.  So while the full 64 bit
  registers and instruction set are available, C pointers are 32 bits
  long and Zephyr is constrained to run in the bottom 4G of memory.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-01-11 15:18:52 -05:00
Jan Van Winkel
cbfcae7a1f gui: Added glue logic for LittlevGL GUI library
Added glue logic to interface Zephyr with LittlevGL GUI library

This includes:
 * KConfig options for all lvgl options
 * Kernel & user space memory management
 * Zephyr to lvgl FS call mapping
 * Color space conversion function

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
2019-01-07 16:05:35 -05:00
Flavio Ceolin
0c4bb833de lib: posix: Make statements evaluate boolean expressions
MISRA-C requires that the if statement has essentially Boolean type.

MISRA-C rule 14.4

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2019-01-07 08:52:07 -05:00
Flavio Ceolin
76b3518ce6 kernel: Make statements evaluate boolean expressions
MISRA-C requires that the if statement has essentially Boolean type.

MISRA-C rule 14.4

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2019-01-07 08:52:07 -05:00
Alistair Francis
41dfc4d478 libc: riscv: Fix the RISC-V ifdef
Following the standard GCC RISC-V convetion use __riscv for the RISC-V
specific define:
41d6b10e96/gcc/config/riscv/riscv-c.c (L37)

Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
2019-01-03 18:15:53 -05:00
Yannis Damigos
33f1951c9d crc: Move crc7 into CRC single header
Move crc7 into CRC single header

Signed-off-by: Yannis Damigos <giannis.damigos@gmail.com>
2018-12-30 16:24:10 -05:00
bbafc36b1c lib: add crc7.
Used as a checksum on command messages when talking with MMC cards.

Implemented using the unwound bytewise implementation from
https://en.wikipedia.org/wiki/Computation_of_cyclic_redundancy_checks
which is a good mix of size and speed.

The API and naming matches lib/crc7.c in Linux.

Signed-off-by: Michael Hope <mlhx@google.com>
2018-12-30 16:24:10 -05:00
Andy Ross
0d4954fafc lib/cmsis_rtos_v2: Fix overflow in osKernelGetInfo()
If any of the Zephyr version numbers went beyond 99, the "%2d" printf
specifiers would expand to fit and the string would run over the
memory on the stack used for os_str[].

Recent GCC versions (remember native_posix and x86_64 use the host
compiler) were actually detecting this and correctly issuing a warning
(but only if the 3-digit char value would overflow the actual array
size!), which was breaking sanitycheck for me on Fedora 28 and Ubuntu
18.04 build hosts.  Pretty impresive warning.

As it happens this was wasteful anyway; we were spending bytes on the
stack (and in rodata to store the constant which, and the cycles
needed to copy it into place on the stack where it would be
overwritten immediately) when we could just snprintf() directly into
the buffer the user gave us.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-12-26 09:41:41 -05:00
Rajavardhan Gundi
41aa164c85 lib/cmsis_rtos_v2: Implement support for Thread Flags APIs
Thread Flags are used to trigger execution states between threads.
These APIs provide functionalities like set, clear and wait.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-12-20 12:23:22 +01:00
Rajavardhan Gundi
fa499b353d lib/cmsis_rtos_v2: Implement support for Event Flag APIs
Events are used to trigger execution states between threads.
These APIs provide functionalities like event set, clear and
wait.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-12-20 12:23:22 +01:00
Rajavardhan Gundi
27f4cd2356 lib/cmsis_rtos_v2: Implement support for Message Queue APIs
These APIs provide message queue functionality like create,
put and get.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-12-20 12:23:22 +01:00
Rajavardhan Gundi
e5c848878a lib/cmsis_rtos_v2: Implement support for mempool APIs
These APIs allow creating, allocating and freeing of mempools.

Note: "Mempool" in CMSIS actually means memslabs in Zephyr.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-12-20 12:23:22 +01:00
Rajavardhan Gundi
b39f6268c1 lib/cmsis_rtos_v2: Implement support for semaphore APIs
These APIs allow creating, acquiring, releasing and deleting
of semaphores.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-12-20 12:23:22 +01:00
Rajavardhan Gundi
19b57ce2e8 lib/cmsis_rtos_v2: Implement support for mutex APIs
These APIs allow creating, acquiring, releasing and deleting
the mutexes.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-12-20 12:23:22 +01:00
Rajavardhan Gundi
0285c69a3a lib/cmsis_rtos_v2: Implement support for timer APIs
These APIs provide the support of virtual timers. All timers
can be started, restarted, or stopped. Timers can be configured
as one-shot or periodic.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-12-20 12:23:22 +01:00
Rajavardhan Gundi
7d499e3471 lib/cmsis_rtos_v2: Implement support for generic wait APIs
APIs to introduce wait i.e osDelay and osDelayUntil are defined
here. They are analogous to k_sleep in the kernel.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-12-20 12:23:22 +01:00
Rajavardhan Gundi
e3fe0cafab lib/cmsis_rtos_v2: Implement support for Kernel APIs
Implement support for Kernel management APIs like
osKernelInitialize, osKernelGetTickCount, osKernelGetSysTimerCount
etc.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-12-20 12:23:22 +01:00
Rajavardhan Gundi
4af7b6ccf4 lib/cmsis_rtos_v2: Implement support for thread APIs
These APIs allow defining, creating and controlling of thread
related functionalities.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-12-20 12:23:22 +01:00
Flavio Ceolin
17c7db6bbe lib: posix: Explicitly ignoring return of memcpy
According with MISRA-C the value returned by a non-void function has
to be used. As memcpy return is almost useless, we are explicitly
ignoring it.

MISRA-C rule 17.7

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-12-19 14:37:25 +01:00
Paul Sokolovsky
4a04ed20fd lib/fdtable: Add ioctl() and fcntl() implementations
ioctl() just dispatches to the corresponding vmethod of an fd.
fcntl() handles fdtable-level operations (so far doesn't handle
actually, returning "not implemented" error), and forwards
fd-specific operations to ioctl vmethod just the same (i.e.
ioctl and fcntl operations share the same namespace, but otherwise
disjoint).

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-12-14 13:21:31 +02:00
Paul Sokolovsky
13b38ed686 lib: fdtable: Change ioctl vmethod signature to take va_list
As extend fdtable usage to more cases, there regularly arises a need
to forward ioctl/fcntl arguments to another ioctl vmethod, which is
complicated because it defined as taking variadic arguments. The only
portable solution is to convert variadic arguments to va_list at the
first point of entry from client code, and then pass va_list around.

To facilitate calling ioctl with variadic arguments from system code,
z_fdtable_call_ioctl() helper function is added.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-12-14 13:21:31 +02:00
Robert Lubos
03df2bb888 lib: fdtable: Add function to retrieve fd vtable
Add function that allows to obtain both object and vtable of the file
descriptor.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-12-10 11:38:13 +02:00
Flavio Ceolin
c1eee25a98 lib: rbtree: Do not use lowercase l for literal suffix
Use suffix L to remove potential ambiguity between digit 1 and letter
l.

MISRA-C rule 7.3

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-12-07 09:06:34 -05:00
Patrik Flykt
186fb94bcb lib: Add 'U' to unsigned variable assignments
Add 'U' to a value when assigning it to an unsigned variable.
MISRA-C rule 7.2

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-12-04 22:51:56 -05:00
Anas Nashif
b8424b4cae crc: deprecate old headers and issue warning when used
Change code to use crc.h instead crc{8,16,32}.h and issue warning when
old headers are used.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2018-12-04 08:52:46 -06:00
Flavio Ceolin
ad90341288 lib: rbtree: Make boolean functions return bool
MISRA-C rule 14.4

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-11-30 08:05:11 -08:00
Flavio Ceolin
1f176e485a libc: ctype: Fix isxdigit function
The last check was never true since 'a' > 'A'.
So, things like isxdigit(Z) would return true.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-11-29 15:28:21 -08:00
Flavio Ceolin
c16b1d08df misra: Ignoring the return atomic_set when not used
The function atomic_set return the previous value of the
target. Sometimes this value is irrelevant, e.g when initializing a
variable.

As MISRA-C rule 17.7 requires that the value returned by a non-void
function must be used, we have to explicitly ignore some cases.

MISRA-C rule 17.7

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-11-29 10:18:59 -08:00
Andrew Boie
2b1d54e897 kernel: add user mode work_q capability
This allows for workqueues to be started in user mode.
No additional kernel objects or system calls are defined
other than starting the workqueue in user mode; for
permission purposes the embedded queue and thread objects
are sufficient.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2018-11-29 09:21:18 -08:00
Andy Ross
7d78122caa lib/mempool: One more spurious -ENOMEM condition
Similar to the last patch, there was a spot in block recombination
where the lock would be released while the combined block was being
held allocated.  That means that when recombining a single top-level
block, it was possible for the entire heap to look allocated.

Make the combination and re-addition of the larger block atomic.
Requires a little surgery to the structure of the code, so this is a
little more involved than the earlier fix.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-11-20 11:35:10 +01:00
Andy Ross
7845e1b01e lib/mempool: Fix spurious -ENOMEM due to agressive latency control
The mempool operations need to be atomic, but because of latency
concerns (the allocator is intended for use in an ISR) the locking was
designed to be as minimal as possible.  And it... mostly got it right.
All the list handling was correctly synchronized.  The merging of four
child blocks into a parent block was atomic.  The splitting of a block
into four children was atomic.

BUT: there was a moment between the allocation of a large block and
the re-addition of its three unused children where the lock was being
released.  This meant that another context (e.g. an ISR that just
fired, interrupting the existing call to k_mem_pool_alloc()) would see
some memory "missing" that wasn't actually allocated.  And if this
happens to have been the top level block, it's entirely possible that
the whole heap looks empty, even though the other allocator might have
been doing only the smallest allocation!

Fix that by making the "remove a block then add back the three
children we don't use" into an atomic step.  We can still relax the
lock between levels as we split the subblocks further.

(Finally, note that this trick allows a somewhat cleaner API as we can
do our "retry due to race" step internally by walking back up the
block size list instead of forcing our caller to do it via that weird
-EAGAIN return value.)

Fixes #11022

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-11-20 11:35:10 +01:00
Rajavardhan Gundi
50c0ff6e8c lib/cmsis_rtos_v1: Use k_is_in_isr instead of _is_in_isr
Use the kernel API k_is_in_isr() instead of the internal
_is_in_isr() function.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-11-15 09:20:57 -05:00
Andrzej Głąbek
20202902f2 dts_fixups: Use DT_ prefix in all defined labels not related to Kconfig
These changes were obtained by running a script  created by
Ulf Magnusson <Ulf.Magnusson@nordicsemi.no> for the following
specification:

1. Read the contents of all dts_fixup.h files in Zephyr
2. Check the left-hand side of the #define macros (i.e. the X in
   #define X Y)
3. Check if that name is also the name of a Kconfig option
   3.a If it is, then do nothing
   3.b If it is not, then replace CONFIG_ with DT_ or add DT_ if it
       has neither of these two prefixes
4. Replace the use of the changed #define in the code itself
   (.c, .h, .ld)

Additionally, some tweaks had to be added to this script to catch some
of the macros used in the code in a parameterized form, e.g.:
- CONFIG_GPIO_STM32_GPIO##__SUFFIX##_BASE_ADDRESS
- CONFIG_UART_##idx##_TX_PIN
- I2C_SBCON_##_num##_BASE_ADDR
and to prevent adding DT_ prefix to the following symbols:
- FLASH_START
- FLASH_SIZE
- SRAM_START
- SRAM_SIZE
- _ROM_ADDR
- _ROM_SIZE
- _RAM_ADDR
- _RAM_SIZE
which are surprisingly also defined in some dts_fixup.h files.

Finally, some manual corrections had to be done as well:
- name##_IRQ -> DT_##name##_IRQ in uart_stm32.c

Signed-off-by: Andrzej Głąbek <andrzej.glabek@nordicsemi.no>
2018-11-13 10:44:42 -06:00
Paul Sokolovsky
53153405aa lib: fdtable: File descriptor table should reside in kernel memory
Private kernel data structure which should not be accessible to
userspace threads. Mark with __kernel.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-11-08 09:47:46 -08:00
Paul Sokolovsky
8f690e291b lib: fdtable: FD method tables should be const.
FD method tables contain function pointers, and thus should be
const and reside in ROM. This patch fixes all cases of FD vtable
definitions: for POSIX FS API and for sockets.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-11-08 09:47:46 -08:00
Flavio Ceolin
19301849e8 lib: Normalize if/else statements
Enforce braces in all if/else statements. This is part of MISRA-C rule
15.6.

MISRA-C rule 15.6

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-11-06 16:20:15 -05:00
Paul Sokolovsky
79ea613a32 lib/fdtable: Can call zephyr_write() only for CONFIG_NEWLIB_LIBC
If we don't have Newlib, the more or less POSIX library, it's unclear
how to deal with POSIX stdin/stdout/stderr at all.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-11-04 22:04:11 +01:00
Paul Sokolovsky
65a33bd0c0 lib: fdtable: Define standard I/O file descriptors if CONFIG_POSIX_API
This is simplistic implementation which just redirects to (likewise
simplistic) implementation in lib/libc/newlib/libc-hooks.c. This
should be replaced with bindings to "real console", but what should
be a "real console" is so far discussed, at the RFC stage.

This implementation goes into the fdtable.c itself to keep all those
things nicely static. (This is again likely will change when we have
"real console", but again, it's so far not clear where it would
belong, so at least avoid creating random files to be deleted later).

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-11-04 22:04:11 +01:00
Paul Sokolovsky
b2fd1a27ac libc: newlib: libc-hooks: s/CONFIG_POSIX_FS/CONFIG_POSIX_API/
read/write/etc. are defined in case CONFIG_POSIX_API is defined, and
we shouldn't provide duplicates.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-11-04 22:04:11 +01:00
Paul Sokolovsky
b6e58d54c9 lib: posix: fs: Convert to use generic fdtable
All the handling of POSIX file descriptors is now done by fdtable.c.
fs.c still manages its own table of file structures of the underlying
fs lib.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-11-04 22:04:11 +01:00
Paul Sokolovsky
f484bbaa26 lib: posix: Implement generic file descriptor table
The table allows to wrap read/write (i.e. POSIX-compatible) semantics
of any I/O object in POSIX-compatible fd (file descriptor) handling.
Intended I/O objects include files, sockets, special devices, etc.

The table table itself consists of (underlying obj*, function table*)
pairs, where function table provides entries for read(), write, and
generalized ioctl(), where generalized ioctl handles all other
operations, up to and including closing of the underlying I/O object.

Fixes: #7405

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-11-04 22:04:11 +01:00
Flavio Ceolin
aecd4ecb8d kernel: Change k_poll_signal api
k_poll_signal was being used by both, struct and function. Besides
this being extremely error prone it is also a MISRA-C violation.
Changing the function to contain a verb, since it performs an action
and the struct will be a noun. This pattern must be formalized and
followed and across the project.

MISRA-C rules 5.7 and 5.9

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-11-04 11:37:24 -05:00
Punit Vara
2cf270758e lib: posix: Return errno code
Return EINVAL errno when argument doesn't refer to
valid semaphore.

partly fixes #9993

Signed-off-by: Punit Vara <punit.vara@intel.com>
2018-10-31 12:15:22 -04:00
Adithya Baglody
d4f6485efd lib: rbtree: Fixed incorrect return type for rb_contains
The API rb_contains computes a boolean value but returns an interger
type.

Signed-off-by: Adithya Baglody <adithya.nagaraj.baglody@intel.com>
2018-10-31 08:44:47 -04:00
Kumar Gala
d37e328080 lib: mempool: Fix compile warning
Fix a compile warning if we build using int types defined to match the
compiler.  We get the following warnings:

lib/mempool/mempool.c: In function ‘sys_mem_pool_alloc’:
lib/mempool/mempool.c:317:48: warning: passing argument 3 of ‘_sys_mem_pool_block_alloc’ from incompatible pointer type [-Wincompatible-pointer-types]
  if (_sys_mem_pool_block_alloc(&p->base, size, &level, &block,
                                                ^
lib/mempool/mempool.c:221:5: note: expected ‘u32_t * {aka long unsigned int *}’ but argument is of type ‘int *’
 int _sys_mem_pool_block_alloc(struct sys_mem_pool_base *p, size_t size,
     ^~~~~~~~~~~~~~~~~~~~~~~~~
lib/mempool/mempool.c:317:56: warning: passing argument 4 of ‘_sys_mem_pool_block_alloc’ from incompatible pointer type [-Wincompatible-pointer-types]
  if (_sys_mem_pool_block_alloc(&p->base, size, &level, &block,
                                                        ^
lib/mempool/mempool.c:221:5: note: expected ‘u32_t * {aka long unsigned int *}’ but argument is of type ‘int *’
 int _sys_mem_pool_block_alloc(struct sys_mem_pool_base *p, size_t size,
     ^~~~~~~~~~~~~~~~~~~~~~~~~

Make local variables block & level u32_t to match what
_sys_mem_pool_block_alloc expects.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2018-10-29 10:52:00 -04:00
Kumar Gala
54bdd7aee0 Revert "libc: minimal: Add error codes for key management"
This reverts commit 25fb2302f1.

The bluetooth l2cap code was using these errno values but changed to
using more standard EPERM instead, so lets remove the defines since
nothing uses them.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2018-10-29 10:12:23 +00:00
Paul Sokolovsky
52aa8061c0 libc: newlib: libc-hooks: Consistently use const void* as arg to write
write() function is not supposed to change buffer passed to it, so
propagate const pointer param to all write-like functions used/defined
in this file.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-10-22 15:01:12 +01:00
Paul Sokolovsky
5955996e7a libc: minimal: fcntl.h: Fix include guards for the current path
The file was moved, but include guards still referred to the old
path.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-10-10 16:38:13 -04:00
Paul Sokolovsky
c80f61a1a7 libc: minimal: Add dummy sys/stat.h [REVERTME]
Some third-party components include this file without really needing
any symbols from it. Presence of this file allows to build them
against minimal libc, whereas previously they forced Newlib.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-10-10 13:31:00 -04:00
Paul Sokolovsky
8e1d78c357 libc: minimal: Make fcntl.h be at top level, not sys/fcntl.h
Placing it at sys/fcntl.h was due to mimicking internal newlib's
layout, but what we need is this file at the standard location,
for reuse.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-10-09 15:44:59 -04:00
Krzysztof Chruscinski
26031f7bfd lib: ring_buffer: add raw byte access mode
Extended ring buffer to allow storing raw bytes in it. API has been
extended keeping 'data item' mode untouched.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2018-10-09 13:58:44 -04:00
Krzysztof Chruscinski
ff5f00f2c3 lib: ring_buffer: Rename sys_ring_buf_ to ring_buf_item_
Deprecate API prefixed with sys_ring_buf_ and rename it
to ring_buf_item_ since this API is not a typical ring buffer
but ring buffer of data items (metadata + 32bit words).

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2018-10-09 13:58:44 -04:00
Paul Sokolovsky
7f9127578b include: posix: unistd: Fix prototypes and dependency
For read/write/lseek, use size_t and off_t types, as mandated by
POSIX:
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html

Also, prototypes of unistd.h functions should not depend on
CONFIG_POSIX_FS, as (many) of them deal with generic I/O, not with
files in filesystem per se.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-10-09 08:19:44 -04:00
Anas Nashif
f8d439d36e kernel: move malloc handling to new logger
Make this part of the kernel log domain, it is used by the kernel.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2018-10-08 17:49:12 -04:00
Alexander Polleti
aec8971bfd crc8: fix const correctness
Declares the array with constant data const.

Tested with tests/unit/lib/crc.

Signed-off-by: Alexander Polleti <metapsychologe@yahoo.de>
2018-10-04 16:37:21 -05:00
Rajavardhan Gundi
7fdfe03ad0 lib/cmsis_rtos_v1: Fix some Kconfig inconsistencies
Fixed some Kconfig inconsistencies around THREAD_CUSTOM_DATA,
POLL and NUM_PREEMPT_PRIORITIES.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-10-02 14:08:33 -07:00
Paul Sokolovsky
7e9263098a lib: posix: Build pthreads files depending on CONFIG_PTHREAD_IPC
If application didn't request pthreads support, don't build related
files.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-10-02 10:51:52 -07:00
Paul Sokolovsky
0fac0cd94a lib: posix: fs: Don't depend on pthreads
File system access isn't related to pthreads.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-10-02 10:51:52 -07:00
Paul Sokolovsky
d77c96b473 lib: posix: pthread_common: Don't depend on pthreads
Contrary to its name, doesn't depend on pthreads.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-10-02 10:51:52 -07:00
Paul Sokolovsky
8dc69e09da lib: posix: Add top-level define for all POSIX APIs - CONFIG_POSIX_API
It so happened that previously CONFIG_PTHREAD_IPC served this role.
But pthreads and IPC is only parts of POSIX, orthogonal to other
services.

Move CONFIG_POSIX_FS, etc. out from CONFIG_PTHREAD_IPC.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-10-02 10:51:52 -07:00
Paul Sokolovsky
c152ebd634 include: posix: Split dirent.h from unistd.h
From POSIX
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/dirent.h.html

"""
The <dirent.h> header shall define the following type:
DIR

...

also define the structure dirent
"""

etc.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-10-02 10:51:52 -07:00
Spoorthi K
df5c29d3d1 lib: POSIX: Add check for deadlock in pthread_join
Calling pthread_join() with current thread would lead
to deadlock. Adding check for it and to return
appropriate error code.

Signed-off-by: Spoorthi K <spoorthi.k@intel.com>
2018-09-29 06:01:39 -04:00
Mark Ruvald Pedersen
d67096da05 portability: Avoid void* arithmetics which is a GNU extension
Under GNU C, sizeof(void) = 1. This commit merely makes it explicit u8.

Pointer arithmetics over void types is:
 * A GNU C extension
 * Not supported by Clang
 * Illegal across all ISO C standards

See also: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html

Signed-off-by: Mark Ruvald Pedersen <mped@oticon.com>
2018-09-28 07:57:28 +05:30
Niranjhana N
d3ab9a37d8 lib: posix: remove return error code
ENOTSUP is not being used correctly in
pthread_attr_setschedparam(), hence
replaced its check for EINVAL instead.

Signed-off-by: Niranjhana N <niranjhana.n@intel.com>
2018-09-28 06:53:24 +05:30
Niranjhana N
7e6c103ff5 lib: posix: fix return error code
Added EAGAIN error code in pthread_create()
with fixing the EINVAL return as it is
limited to attribute checking.

Signed-off-by: Niranjhana N <niranjhana.n@intel.com>
2018-09-28 06:53:24 +05:30
Niranjhana N
482579e491 lib: posix: add error code return
Added return of ESRCH error code in
pthread_getschedparam() when the
specified thread could not be found.

Signed-off-by: Niranjhana N <niranjhana.n@intel.com>
2018-09-28 06:53:24 +05:30
Flavio Ceolin
02ed85bd82 kernel: sched: Change boolean APIs to return bool
Change APIs that essentially return a boolean expression  - 0 for
false and 1 for true - to return a bool.

MISRA-C rule 14.4

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-09-28 06:28:41 +05:30
Nils Montenegro
25fb2302f1 libc: minimal: Add error codes for key management
This adds EKEY* codes, as defined by Linux, using the same numeric
values as Linux.

Signed-off-by: Nils Montenegro <nils.montenegro@nordicsemi.no>
2018-09-26 00:55:21 +05:30
Niranjhana N
1e152ff20e lib: cmsis_rtos_v1: do null check before use
Moving a thread_def null check to top before
using the structure's elements.

Signed-off-by: Niranjhana N <niranjhana.n@intel.com>
2018-09-21 08:49:27 -04:00
Niranjhana N
4462dfabb6 lib: posix: fix couple of race conditions
Added locks around thread state accesses in
pthread_create() to avoid race conditions.

Signed-off-by: Niranjhana N <niranjhana.n@intel.com>
2018-09-21 00:24:22 -04:00
Flavio Ceolin
4218d5f8f0 kernel: Make If statement have essentially Boolean type
Make if statement using pointers explicitly check whether the value is
NULL or not.

The C standard does not say that the null pointer is the same as the
pointer to memory address 0 and because of this is a good practice
always compare with the macro NULL.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-09-18 13:57:15 -04:00
Flavio Ceolin
67ca176754 headers: Fix headers across the project
Any word started with underscore followed by and uppercase letter or a
second underscore is a reserved word according with C99.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-09-17 15:49:26 -04:00
Flavio Ceolin
98c64b6d92 kernel: Change _reschedule signature
_reschedule return's value is not used anywhere, except erroneously by
pthread_barrier_wait.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-09-14 16:55:37 -04:00
Flavio Ceolin
da49f2e440 coccicnelle: Ignore return of memset
The return of memset is never checked. This patch explicitly ignore
the return to avoid MISRA-C violations.

The only directory excluded directory was ext/* since it contains
only imported code.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-09-14 16:55:37 -04:00
Paul Sokolovsky
da4c00c021 libc: minimal: Add sys/fcntl.h enough to compile net/lib/sockets
Contains defines enough to compile BSD Sockets subsystem. Values are
compatible with Newlib.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-09-13 14:29:17 -04:00
David Brown
9d6f1c600f lib: posix: clock: Implement clock_settime
Set a base time, using the current uptime.

Signed-off-by: David Brown <david.brown@linaro.org>
2018-09-11 09:21:52 -04:00
David Brown
9921eb329d lib: posix: clock: Add read of CLOCK_REALTIME
Add a private variable `rt_clock_base` that can be used to determine a
real-time clock by using the `k_uptime_get` clock.  Once `clock_settime`
is added, this can allow us to have a meaningful real time clock.

Signed-off-by: David Brown <david.brown@linaro.org>
2018-09-11 09:21:52 -04:00
David Brown
e0dda1b4b0 lib: posix: clock: Add gettimeofday() call
Provide an implementation of gettimeofday().  This uses clock_gettime()
with the CLOCK_REALTIME parameter, which is currently unimplemented, but
will allow clients to call this function once this functionality has
been implemented.

Signed-off-by: David Brown <david.brown@linaro.org>
2018-09-11 09:21:52 -04:00
Rajavardhan Gundi
9fed7d6338 lib/cmsis_rtos_v1: Use k_mbox_async_put instead of k_mbox_put
Use the asynchronous version of mbox_put instead of the
synchronous one. Also, add an error check in osMailPut.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-09-11 09:03:03 -04:00
Rajavardhan Gundi
8be1cb3b61 lib/cmsis_rtos_v1: Fix boundary conditions in the signals module
Fixed the boundary conditions around osFeature_Signals.
Fixes #9857.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-09-09 11:49:53 -04:00
Rajavardhan Gundi
0aa47a7c4f lib/cmsis_rtos_v1: Check for mutex owner in cmsis layer
Check for the mutex owner in the cmsis layer itself while releasing.
Fixes #9574.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-09-09 11:05:23 -04:00
Praful Swarnakar
bbad5c3fad lib: cmsis_rtos_v1: Minor refactor of CMSIS implemetation
Add few missing NULL checks to avoid crash. Also, minor
refactor of signal code and disable osFeature_Wait to
signify osWait function not implemented.

Signed-off-by: Praful Swarnakar <praful.swarnakar@intel.com>
2018-09-03 12:45:42 -04:00
Niranjhana N
a08794091f lib: cmsis_rtos_v1: remove unhit return case
k_msg_get returns only three possible values, and
osErrorValue is not in osMessageGet spec, hence
removing this unhit else case.

Signed-off-by: Niranjhana N <niranjhana.n@intel.com>
2018-08-30 01:11:08 -04:00
Rajavardhan Gundi
ac787e0e97 lib/posix: Use static allocation for posix_thread objects
The memory occupied by posix_thread objects are not significant.
Hence, no point in using dynamic allocation.

Addresses #8717.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-08-29 15:14:05 -04:00
Spoorthi K
0b8792c05c lib/cmsis_rtos_v1: Check if osKernelStart() is called from ISR
Check if osKernelStart() is called from ISR and return error code
appropriately.

Signed-off-by: Spoorthi K <spoorthi.k@intel.com>
2018-08-28 19:19:06 -04:00
Rajavardhan Gundi
f72c4c5236 lib/cmsis_rtos_v1: Remove redundant stack size check
stacksize is an unsigned integer and hence there's no need to
check whether it is >= 0 since it is always true. This fixes
the Github issue #9637.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-08-27 04:41:11 -07:00
Niranjhana N
845fdbb7c0 lib: cmsis_rtos_v1: replace an else case
Replace an else-if case in osSemaphoreWait with
else to account for both EBUSY and EAGAIN return
values from k_sem_take. The return value would be
0 for osSemaphoreWait in both cases.

Signed-off-by: Niranjhana N <niranjhana.n@intel.com>
2018-08-26 08:56:17 -07:00
Andy Ross
71ca65306c mempool: Fix bit pointer state for N_MAX > 31
When a mempool is created with a large number of maximum-size blocks,
the logic for initializing max_inline_level (i.e. when to union the
bitmask with the pointer and when to use the pointer directly) was
wrong.  The default state was "zero", which implies that level 0
should be inlined, but that's wrong with >32 base blocks.
Additionally, the type was unsigned, making the "level zero is a
pointer" situation impossible to represent.

Fixes #6727

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-08-25 06:57:37 -07:00
Niranjhana N
411662d344 lib: cmsis_rtos_v1: fix couple of nonconformities
Add osErrorTimeoutResource as return value when message
cannot be put in queue during waiting period. Also set
message value only when message is received.

Signed-off-by: Niranjhana N <niranjhana.n@intel.com>
2018-08-20 17:51:29 -07:00
Flavio Ceolin
5c79101f30 constants: Use uppercase to indicate long
Several code guidelines recommend using uppercase L instead of letter
l (ell) because it can easily be confused with the digit 1 (one).

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-08-16 19:47:41 -07:00
Praful Swarnakar
2fc836d2fe lib: cmsis_rtos_v1: Fix timeout calculation in signals
Fix the osSignalWait timeout calculation in cases when
waiting on more than one signal event.

Signed-off-by: Praful Swarnakar <praful.swarnakar@intel.com>
2018-08-16 06:25:10 -07:00
Ulf Magnusson
8cf8db3a73 Kconfig: Use a short, consistent style for prompts
Consistently use

    config FOO
            bool/int/hex/string "Prompt text"

instead of

    config FOO
            bool/int/hex/string
            prompt "Prompt text"

(...and a bunch of other variations that e.g. swapped the order of the
type and the 'prompt', or put other properties between them).

The shorthand is fully equivalent to using 'prompt'. It saves lines and
avoids tricking people into thinking there is some semantic difference.

Most of the grunt work was done by a modified version of
https://unix.stackexchange.com/questions/26284/
how-can-i-use-sed-to-replace-a-multi-line-string/26290#26290, but some
of the rarer variations had to be converted manually.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2018-08-15 04:10:10 -07:00
Rajavardhan Gundi
b5df23e423 lib/cmsis_rtos_v1: Implement support for Signal Event APIs
Signals are used to trigger execution states between threads.
These APIs provide functionalities like signal set, clear and
wait.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-08-13 13:08:07 -07:00
Rajavardhan Gundi
3bb243c6bb lib/cmsis_rtos_v1: Implement support for Message Queue APIs
These APIs provide message queue functionality like create,
put and get.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-08-13 13:08:07 -07:00
Rajavardhan Gundi
7c67140baf lib/cmsis_rtos_v1: Implement support for Mail Queue APIs
These APIs provide mail queue functionality like create, put and get.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-08-13 13:08:07 -07:00
Rajavardhan Gundi
76fc443b1e lib/cmsis_rtos_v1: Implement support for kernel APIs
These APIs allow for checking whether the kernel is initialized,
started or running.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-08-13 13:08:07 -07:00
Rajavardhan Gundi
aff8c51128 lib/cmsis_rtos_v1: Implement support for mempool APIs
These APIs allow creating, allocating and freeing
of mempools.

Note: "Mempool" in CMSIS actually means memslabs in Zephyr.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-08-13 13:08:07 -07:00
Rajavardhan Gundi
45e67150f6 lib/cmsis_rtos_v1: Implement support for semaphore APIs
These APIs allow creating, acquiring, releasing and deleting
of semaphores.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-08-13 13:08:07 -07:00
Rajavardhan Gundi
055da73184 lib/cmsis_rtos_v1: Implement support for mutex APIs
These APIs allow creating, acquiring, releasing and deleting
the mutexes.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-08-13 13:08:07 -07:00
Rajavardhan Gundi
7d3a53900a lib/cmsis_rtos_v1: Implement support for timer APIs
These APIs provide the support of virtual timers. All timers
can be started, restarted, or stopped. Timers can be configured
as one-shot or periodic.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-08-13 13:08:07 -07:00
Rajavardhan Gundi
7e5f0b7bff lib/cmsis_rtos_v1: Implement support for osKernelSysTick
This API is used to fetch the kernel system timer as 32-bit value.
This is analogous to k_cycle_get_32 in the kernel.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-08-13 13:08:07 -07:00
Rajavardhan Gundi
2d7619ecf8 lib/cmsis_rtos_v1: Implement support for osDelay
This API is used to specify delay in ms. This is analogous
to k_sleep in the kernel.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-08-13 13:08:07 -07:00
Rajavardhan Gundi
ccd1c21824 lib/cmsis_rtos_v1: Implement support for thread APIs
These APIs allow defining, creating and controlling of thread
related functionalities.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-08-13 13:08:07 -07:00
Andrew Boie
3641c25df9 libc: minimal: fix calloc()
calloc() wasn't zeroing out the allocated memory as it
is supposed to.

Fixes: #9221

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2018-08-01 19:40:57 -07:00
Andrew Boie
12e6aadcb0 lib: newlib: add read/write syscalls
The read/write implementations call directly into the console drivers
using the hook mechanism, causing faults if invoked from user mode.

Add system calls for read() and write() such that we do a privilege
elevation first.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2018-07-25 14:57:51 -04:00
Andrew Boie
bc94cc1832 libc: minimal: add console system calls
The stdout console implementations for minimal libc call directly into
the various console drivers (depending on what specifc hooks are
registered) causing faults when invoked from user mode. This happens,
for example, when using printf() which eventually ends up calling
fputc().

The proper solution is to ensure privileges have been elevated before
the _stdout_hook is called. This was already done for printk().

puts() and fputs() have now been re-defined in terms of the
fputc() and fwrite() functions, which are now system calls.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2018-07-25 14:57:51 -04:00
Andrew Boie
6a8649f806 libc: minimal: add malloc functions
We base the malloc() implementation on a common sys_mem_pool whose
size is specified by Kconfig.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2018-07-20 11:41:08 -07:00
Andrew Boie
7f4d006959 kernel: fix errno access for user mode
The errno "variable" is required to be thread-specific.
It gets defined to a macro which dereferences a pointer
returned by a kernel function.

In user mode, we cannot simply read/write the thread struct.
We do not have thread-local storage mechanism, so for now
use the lowest address of the thread stack to store this
value, since this is guaranteed to be read/writable by
a user thread.

The downside of this approach is potential stack corruption
if the stack pointer goes down this far but does not exceed
the location, since a fault won't be generated in this case.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2018-07-19 16:44:59 -07:00
Niranjhana N
414c39fc94 posix: add pthread_key and pthread_once APIs
Added 4 new pthread_key APIs for thread-specific data
key creation, deletion, setting and getting the values.

Added a key list to the posix_struct for threads.

Added pthread_once API.

Signed-off-by: Niranjhana N <niranjhana.n@intel.com>
2018-07-19 08:46:33 -04:00
Olivier Martin
e7ae7334db lib/crc: Add CRC32 support
It only adds CRC32 IEEE support at the moment.

Signed-off-by: Olivier Martin <olivier.martin@proglove.de>
2018-07-18 10:00:12 -04:00
Kumar Gala
16ff8ca2c2 libc: newlib: Enable extended linux errno defines
We utilize defines like -ESHUTDOWN in the network stack.  To support
this errno value with newlib we need to enable
__LINUX_ERRNO_EXTENSIONS__.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2018-07-16 11:33:07 -05:00
Kumar Gala
9aebe8b466 lib: json: Fix warning when building with newlib
If we use newlib the isdigit (and other similar functions) return an
error as char can possibly be viewed as signed:

usr/include/ctype.h:57:54: error: array subscript has type ‘char’ [-Werror=char-subscripts]
 #define __ctype_lookup(__c) ((__ctype_ptr__+sizeof(""[__c]))[(int)(__c)])

Explicity cast to unsigned char so we deal with both this warning and
possible warning when -Wpointer-sign is enabled.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2018-07-12 16:01:30 -05:00
Kumar Gala
e66da3f9e0 libc: minimal: Add support for %F conversion specifiers
For some reason %F wasn't supported initially.  Its simple enough to
handle the case difference in infinity and NaN handling to add support
for %F.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2018-07-12 14:57:52 -05:00
Kumar Gala
409c9e751f libc: minimal: Fix support for -nan
We were only handling the sign bit for infinity, but not NaN.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2018-07-12 14:57:52 -05:00
Kumar Gala
96ea7ab7d1 libc: minimal: Fix handling of %f conversion specifiers for inf & nan
The C standard says that %f should use '[-]inf' or '[-]infinity' (which
style is implementation defined) for infinity handling and '[-]nan' for
NaN.

We where adding a '+' and had the wrong case for 'inf' and 'nan'.

Before -> After

+INF -> inf
-INF -> -inf
 NaN -> nan

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2018-07-12 14:57:52 -05:00
Kumar Gala
e6f4f623b7 libc: minimal: Fix handling of floating point exponent
For %{e,E,g,G} conversion specifiers the C standard says the exponent
contains at least two digits, and only as many digits are necessary.  So
instead of 1.234000e-001 we should have 1.234000e-01.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2018-07-12 14:57:52 -05:00
Subramanian Meenakshi Sundaram
5193b5576f lib: posix: Fix Out-of-bound write to char array
memcpy copies upto (rc-1)th index but the write of NULL character
to the string is at (rc+1)th index skipping (rc)th index.
The fix addresses this as well.

CID: 186491

Fixes Issue #8280

Signed-off-by: Subramanian Meenakshi Sundaram <subbu147@gmail.com>
2018-07-03 13:01:58 -05:00
Ulf Magnusson
0785b79ebe lib: kconfig: Remove redundant 'default n' properties
Bool symbols implicitly default to 'n'.

A 'default n' can make sense e.g. in a Kconfig.defconfig file, if you
want to override a 'default y' on the base definition of the symbol. It
isn't used like that on any of these symbols though.

Also simplify the default on STDOUT_CONSOLE. Defaults can be arbitrary
expressions, not just fixed values.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2018-06-22 15:12:48 -04:00
Leandro Pereira
2d71236a36 lib: libc: minimal: Get rid of the bit (256-byte) charmap table
The charmap table used by strncasecmp() not only used precious 256
bytes of ROM, it also had wrong mappings outside the ASCII range
(123..218).

Rewrite strncasecmp() to call tolower() instead; might be a tiny wee
little bit slower than the current version, but it's not used in any
performance-sensitive parts of the code to justify the waste.

This reduces the ROM footprint for the ws_echo_server sample by ~224
bytes.

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2018-06-19 14:33:34 -07:00
Alex Tereschenko
3c1a78ea0d cmake: replace PROJECT_SOURCE_DIR with ZEPHYR_BASE
Both variables were used (with the same value) interchangeably
throughout CMake files and per the discussion in GH issue,
ZEPHYR_BASE is preferred.

Also add a comment with explanation of one vs. the other.

Tested by building hello_world for several boards ensuring no errors.

Fixes #7173.

Signed-off-by: Alex Tereschenko <alext.mkrs@gmail.com>
2018-06-18 15:25:55 -04:00
Paul Sokolovsky
6245d6c47b libc: minimal: Add typedefs for "least" types
Based on feedback integrating with TI SimpleLink HAL.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-06-18 10:23:05 -04:00
Yasushi SHOJI
2e0af08e55 build: remove unused CMakeLists.txt
lib/libc/minimal/source/CMakeLists.txt and
lib/libc/minimal/source/stdout/CMakeLists.txt was introduced in
12f8f7616 but it is not used by the build system.  CMakeLists.txt in
the parent dir lib/libc/minimal/CMakeLists.txt adds C files to the
target with the lines like:

    ${CMAKE_CURRENT_SOURCE_DIR}/source/stdlib/atoi.c
    ${CMAKE_CURRENT_SOURCE_DIR}/source/stdlib/strtol.c

To make other empty CMakeLists.txt explicit, this commit adds a
comment line to them.

Signed-off-by: Yasushi SHOJI <y-shoji@ispace-inc.com>
2018-06-14 15:02:02 -04:00
Ulf Magnusson
99cef4c60d lib: Fix malformed JSON_LIBARY Kconfig default
'default N' should have been 'default n', though they happen to have the
same effect here, due to undefined Kconfig symbols ('N') evaluating to
'n' in a boolean sense.

Kconfig bool symbols implicitly default to 'n', so remove the default
rather than fixing it.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2018-06-13 13:35:56 -04:00
Sebastian Bøe
d94231f66e cmake: libc: minimal: Move sources from 'app' to a new CMake library
The minimal libc source files have been added to 'app'. The Zephyr
build system should not be adding source files to the 'app' library
unless necessary.

This patch creates a new Zephyr CMake Library in lib/libc/minimal and
adds the sources to it.

Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
2018-06-11 17:11:00 -04:00
Paras Jain
bf1e0198a7 lib: posix: fix out-of-bound write
Ensure that write is in buffer limits

Coverity-CID: 186491

Signed-off-by: Paras Jain <parasjain2000@gmail.com>
2018-06-09 08:26:18 -05:00
Ramakrishna Pallala
03a3c992b8 lib: posix: clock: Use k_uptime_get() to compute tv_nsec
Use k_uptime_get() to compute both tv_sec and tv_nsec members
of timespec structure.

Fixes #8009

Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
2018-06-02 16:00:23 -04:00
Ramakrishna Pallala
817e3cd952 lib: posix: Make sure the name string is NULL terminated
Make sure the name string is NULL terminated in the readdir().

CID: 186037

Fixes Issue #7733

Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
2018-06-01 12:46:42 -04:00
Kumar Gala
dd78ab0513 newlib: Fix typo in Kconfig related to NEWLIB_LIBC_ALIGNED_HEAP_SIZE
When we introduced NEWLIB_LIBC_ALIGNED_HEAP_SIZE in commit
42a2c96422.  We accidently had the Kconfig
symbol depend on CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT the leading
'CONFIG_' shouldn't exist.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2018-05-23 12:10:15 -04:00
Andy Ross
f4b6daff4b lib/posix: Port wait_q usage to new API
The pthread mutex changes went in with an adaptation to build with the
new wait queue API, but they did it by using the old dlist hooks
directly through typecasting and union assignment.  That... is sort of
the opposite of the intent to having the new API be abstracted.  The
pthread code worked, but failed once wait queues (on x86) stopped
being dlists.

Simple fix once I saw the problem, anyway.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-05-19 07:00:55 +03:00
Andy Ross
6040bf7773 lib/rbtree: Fix & document insert comparison order
The scheduler priq implementation was taking advantage of a subtle
behavior of the way the tree presents the order of its arguments (the
node being inserted is always first).  But it turns out the tree got
that wrong in one spot.

As this was subtle voodoo to begin with, it should have been
documented first.  Similarly add a little code to the test case to
guarantee this in the future.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-05-19 07:00:55 +03:00
Andy Ross
d33b49d4a3 lib/rbtree: Fix crash condition with empty trees and rb_min/max()
These weren't properly checking the case of an empty tree

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-05-19 07:00:55 +03:00
Leandro Pereira
0f1d30aa67 lib: posix: Do not redefine PATH_MAX in unistd.h
This constant should be defined in limits.h.  Define it in limits.h in
the minimal libc, and use the definition found in newlib's includes.
Values in newlib includes range from 1024 to 4096.

The rationale is that all code should use the same value; having
buffers specified with different sizes will lead to interoperability
and out of bounds array writes.

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2018-05-19 06:59:40 +03:00
Punit Vara
4e3d99ed7e lib: posix: Use default attribute for mutex
Use NULL as argument to intialize attribute values with default
attributes.

Signed-off-by: Punit Vara <punit.vara@intel.com>
2018-05-18 23:02:28 +03:00
Punit Vara
eb8ba696d2 lib: posix: Implement posix mutex APIs
Add posix apis for mutex.

Signed-off-by: Punit Vara <punit.vara@intel.com>
2018-05-18 23:02:28 +03:00
Ramakrishna Pallala
eb0aaca64d lib: posix: Add Posix Style File System API support
Add IEEE 1003.1 Posix Style file system API support.
These API's will internally use corresponding Zephyr
File System API's.

Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
2018-05-18 13:32:36 +03:00
Andy Ross
ccf3bf7ed3 kernel: Fix sloppy wait queue API
There were multiple spots where code was using the _wait_q_t
abstraction as a synonym for a dlist and doing direct list management
on them with the dlist APIs.  Refactor _wait_q_t into a proper opaque
struct (not a typedef for sys_dlist_t) and write a simple wrapper API
for the existing usages.  Now replacement of wait_q with a different
data structure is much cleaner.

Note that there were some SYS_DLIST_FOR_EACH_SAFE loops in mailbox.c
that got replaced by the normal/non-safe macro.  While these loops do
mutate the list in the code body, they always do an early return in
those circumstances instead of returning into the macro'd for() loop,
so the _SAFE usage was needless.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-05-18 01:48:48 +03:00
Andy Ross
ba2405023b lib: rbtree: Add RB_FOR_EACH macro for iterative enumeration
Works mostly like the list enumeration macros.  Implemented by fairly
clever alloca trickery and some subtle "next node" logic.  More
convenient for many uses, can be early-exited, but has somewhat larger
code size than rb_walk().

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-05-17 11:32:20 -07:00
Florian Vaussard
2514f3c837 libc: minimal: fix fwrite()
The implementation of fwrite() in the minimal libc does not increment
the source pointer, and thus always print the same character.

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
2018-05-17 07:53:44 -07:00
Ramakrishna Pallala
bcdfa76ff3 lib: posix: Fix pthread_attr_init() return code
pthread_attr_init() should not return EBUSY as per POSIX spec
so fixed this by return ENOMEM if the attr pointer is NULL.

Also fixed the attribute initialization logic by copying the
init_pthread_attrs to the attr.

Fixes Issue #7480

Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
2018-05-16 23:15:21 +03:00
Adithya Baglody
5133cf56aa kernel: thread: Move out the function _thread_entry() to lib
The _thread_entry() is not really a part of the kernel but a part of
the zephyr's C runtime support library. Hence moving just the
function to lib/thread_entry.c

Signed-off-by: Adithya Baglody <adithya.nagaraj.baglody@intel.com>
2018-05-15 17:48:18 +03:00
Andrew Boie
42a2c96422 newlib: fix heap user mode access for MPU devices
MPU devices that enforce power-of-two alignment now
specify the size of the buffer used for the newlib heap.
This buffer will be properly aligned and a pointer
exposed in a kernel header, such that it can be added
to a user thread's memory domain configuration if
necessary.

MPU devices that don't have these restrictions allocate
the heap as normal.

In all cases, if an MPU/MMU region needs to be programmed,
the z_newlib_get_heap_bounds() API will return the necessary
information.

Given how precious MPU regions are, no automatic programming
of the MPU is done; applications will need to do this as
needed in their memory domain configurations.

On x86, the x86 MMU-specific code has been moved to arch/x86
using the new z_newlib_get_heap_bounds() API.

Fixes: #6814

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2018-05-10 15:09:02 -07:00
Andrew Boie
fc76839b6b x86: grant user mode access to newlib heap
Newlib uses any RAM between _end and the bounds of physical
RAM for the _sbrk() heap. Set up a user-writable region
so that this works properly on x86.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2018-05-09 16:36:36 -07:00
Paul Sokolovsky
a85321229a newlib: libc-hooks: Print "exit" message with newline
Calling POSIX exit() function in Zephyr w/newlib leads to printing
"exit" to stdout followed by infinite loop. That message was
printed without a newline though, leading to confusing artifacts
in the console output.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-05-05 20:13:46 -04:00
Leandro Pereira
0ec79d6853 lib: json: Efficiently pack field name, offset, alignment, type
This trades a little bit over 40 bytes (on x86) of text for a lot of
savings in rodata.  This is accomplished by using bitfields to pack the
field name length, offset, alignment, and the type tag into a single
32-bit unsigned integer instead of scattering this information into
four different integers.

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2018-05-01 15:44:09 -04:00
Ramakrishna Pallala
e7648ba320 lib: posix: pthread_common: Fix potential integer overflow issue
Fix potential overflow of interger expression for by fixing
variable type to s64_t.

CID: 185275

Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
2018-04-30 06:49:42 -04:00
Andy Ross
8a4b2e8cf2 kernel, posix: Move ready_one_thread() to scheduler
The POSIX layer had a simple ready_one_thread() utility.  Move this to
the scheduler API (with a prepended underscore -- it's an internal
API) so that it can be synchronized along with the rest of the
scheduler.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-24 03:57:20 +05:30
Andy Ross
22642cf309 kernel: Clean up _unpend_thread() API
Almost everywhere this was called, it was immediately followed by
_abort_thread_timeout(), for obvious reasons.  The only exceptions
were in timeout and k_timer expiration (unifying these two would be
another good cleanup), which are peripheral parts of the scheduler and
can plausibly use a more "internal" API.

So make the common case the default, and expose the old behavior as
_unpend_thread_no_timeout().  (Along with identical changes for
_unpend_first_thread) Saves code bytes and simplifies scheduler
surface area for future synchronization work.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-24 03:57:20 +05:30
Andy Ross
d89249dbc5 pthread: Respect cooperative thread schedulign in condition variable
Originally, pthread_cond_signal() was written to yield even in
circumstances where the current thread is at a cooperative priority
and would not expect to be context-switched out until it blocks.  This
makes sense, as in most cases you want the newly signaled thread to
get a chance to run as soon as possible.

On further reflection (and also because it complicates the scheduler),
I think that's wrong.  The point to cooperative scheduling is that it
allows the cooperative code to make synchronization assumptions about
exactly when it might yield to other threads, and having arbitrary
APIs be "preemption points" like this complicates that analysis
significantly.

Use _reschedule() like other code does.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-24 03:57:20 +05:30
Andy Ross
15cb5d7293 kernel: Further unify _reschedule APIs
Now that other work has eliminated the two cases where we had to do a
reschedule "but yield even if we are cooperative", we can squash both
down to a single _reschedule() function which does almost exactly what
legacy _Swap() did, but wrapped as a proper scheduler API.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-24 03:57:20 +05:30
Andy Ross
0447a73f6c kernel: include cleanup
Recent changes have eliminated most use of _Swap() in favor of higher
level scheduler abstractions.  We can remove the header too.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-24 03:57:20 +05:30
Andy Ross
e0a572beeb kernel: Refactor, unifying _pend_current_thread() + _Swap() idiom
Everywhere the current thread is pended, the code is going to have to
do a _Swap() soon afterward, yet the scheduler API exposed these as
separate steps.  Unify this pattern everywhere it appears, which saves
some code bytes and gets _Swap() out of the general scheduler API at
zero cost.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-24 03:57:20 +05:30
Andy Ross
8606fabf74 kernel: Scheduler refactoring: use _reschedule_*() always
There was a somewhat promiscuous pattern in the kernel where IPC
mechanisms would do something that might effect the current thread
choice, then check _must_switch_threads() (or occasionally
__must_switch_threads -- don't ask, the distinction is being replaced
by real English words), sometimes _is_in_isr() (but not always, even
in contexts where that looks like it would be a mistake), and then
call _Swap() if everything is OK, otherwise releasing the irq_lock().
Sometimes this was done directly, sometimes via the inverted test,
sometimes (poll, heh) by doing the test when the thread state was
modified and then needlessly passing the result up the call stack to
the point of the _Swap().

And some places were just calling _reschedule_threads(), which did all
this already.

Unify all this madness.  The old _reschedule_threads() function has
split into two variants: _reschedule_yield() and
_reschedule_noyield().  The latter is the "normal" one that respects
the cooperative priority of the current thread (i.e. it won't switch
out even if there is a higher priority thread ready -- the current
thread has to pend itself first), the former is used in the handful of
places where code was doing a swap unconditionally, just to preserve
precise behavior across the refactor.  I'm not at all convinced it
should exist...

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-24 03:57:20 +05:30
Leandro Pereira
3af88642d2 lib: posix: mqueue: Minor formatting cleanups
Remove double spaces before pointer asterisks in some places.

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2018-04-21 06:56:27 -07:00
Leandro Pereira
2a5fb57e95 lib: posix: mqueue: Do not dereference mqd pointer before null check
The compiler can remove the NULL check since the dereference happens
before it (and assume that the pointer is always valid).

Coverity-Id: 185281

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2018-04-21 06:56:27 -07:00
Andy Ross
2ef57f0a1b lib/rbtree: Add a rb_contains() predicate
Returns true if the specified node is in the tree.  Allows the tree to
be used for "set" style semantics along with a lessthan_fn that simply
compares the nodes by their address.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-10 12:31:51 -04:00
Andy Ross
193f4feb84 lib: Red/Black balanced tree data structure
A balanced tree implementation for Zephyr as we grow into bigger
regimes where simpler data structures aren't appropriate.

This implements an intrusive balanced tree that guarantees O(log2(N))
runtime for all operations and amortized O(1) behavior for creation
and destruction of whole trees.  The algorithms and naming are
conventional per existing academic and didactic implementations, c.f.:

https://en.wikipedia.org/wiki/Red%E2%80%93black_tree

The implementation is size-optimized to prioritize runtime memory
usage.  The data structure is intrusive, which is to say the struct
rbnode handle is intended to be placed in a separate struct the same
way other such structures (e.g. Zephyr's dlist list) and requires no
data pointer to be stored in the node.  The color bit is unioned with
a pointer (fairly common for such libraries).  Most notably, there is
no "parent" pointer stored in the node, the upper structure of the
tree being generated dynamically via a stack as the tree is recursed.
So the overall memory overhead of a node is just two pointers,
identical with a doubly-linked list.

Code size above dlist is about 2-2.5k on most architectures, which is
significant by Zephyr standards but probably still worthwhile in many
situations.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-04-10 12:31:51 -04:00
Ramakrishna Pallala
fe46c75d25 lib: posix: Fix integer overflow in timer_gettime
Fix 'Unintentional integer overflow' coverity issue
in timer_gettime().

Coverity-CID: 183038

Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
2018-04-05 16:43:05 -04:00
Ramakrishna Pallala
4226c6d8b2 lib: posix: Fix mutex locking in pthread_cancel
Fix mutex locking sequence in pthread_cancel()

Coverity-CID: 183055

Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
2018-04-05 16:43:05 -04:00
Ramakrishna Pallala
f603e603bb lib: posix: Move posix layer from 'kernel' to 'lib'
Move posix layer from 'kernel' to 'lib' folder as it is not
a core kernel feature.

Fixed posix header file dependencies as part of the move and
also removed NEWLIBC related macros from posix headers.

Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
2018-04-05 16:43:05 -04:00
Andrew Boie
aa6de29c4b lib: user mode compatible mempools
We would like to offer the capability to have memory pool heap data
structures that are usable from user mode threads. The current
k_mem_pool implementation uses IRQ locking and system-wide membership
lists that make it incompatible with user mode constraints.

However, much of the existing memory pool code can be abstracted to some
common functions that are used by both k_mem_pool and the new
sys_mem_pool implementations.

The sys_mem_pool implementation has the following differences:

* The alloc/free APIs work directly with pointers, no internal memory
block structures are exposed to the end user. A pointer to the source
pool is provided for allocation, but freeing memory just requires the
pointer and nothing else.

* k_mem_pool uses IRQ locks and required very fine-grained locking in
order to not affect system latency. sys_mem_pools just use a semaphore
to protect the pool data structures at the API level, since there aren't
implications for system responsiveness with this kind of concurrency
control.

* sys_mem_pools do not support the notion of timeouts for requesting
memory.

* sys_mem_pools are specified at compile time with macros, just like
kernel memory pools. Alternative forms of specification at runtime
will be a later enhancement.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2018-04-05 07:03:05 -07:00
Carles Cufi
20cd4b551b lib: base64: Add statement of changes
As per the Apache v2 License, state changes made to the original code in
the modified version of the files.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2018-03-23 13:54:48 +01:00
Carles Cufi
fb50e81dfd lib: Add base64 library
Since base64 is such a simple and commonly used feature it makes no
sense to build the whole of mbedTLS for it. Instead take the
implementation that comes with mbedTLS and import it as a native library
outside of ext/ for all to use directly.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2018-03-23 05:18:11 -04:00
Anas Nashif
bb64ec2921 lib: move ring_buffer Kconfig to lib/, cleanup lib/Kconfig
* ring_bufffer is in lib, so move the Kconfig out of the kernel.
* move one Kconfig used for json to lib/Kconfig alongside other
  Kconfigs.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2018-03-19 15:37:26 -04:00
dc37f985e9 crc: make crc8_ccitt() match the other CRC functions.
This is a minor change that makes the data pointer const and shifts
the length to a size_t to match the other CRC functions.

Signed-off-by: Michael Hope <mlhx@google.com>
2018-03-10 21:49:07 -05:00
Anas Nashif
7ee8bb9677 build: deprecate ZEPHYR_GCC_VARIANT
We want to support other toolchain not based on GCC, so the variable is
confusing, use ZEPHYR_TOOLCHAIN_VARIANT instead.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2018-02-13 07:44:45 -08:00
a5eb5418d1 crc: add crc16_itu_t which is used in the MMC protocol.
This is the MSB first version of crc16_ccitt.  Use the same name as in
Linux.

Signed-off-by: Michael Hope <mlhx@google.com>
2018-02-07 16:49:52 -05:00
f77c1d53b5 crc: add a faster, chainable version of CRC16/CCITT.
The existing version of crc16_ccitt() is actually CRC-16/AUG-CCITT and
gives different results to Linux, Contiki, and the CRC unit in the
SAM0 SOC.  This version matches Linux.

Note that this is an incompatible API change.

Signed-off-by: Michael Hope <mlhx@google.com>
2018-02-07 16:49:52 -05:00
Paul Sokolovsky
323e8cf069 libc: Kconfig: Enable STDOUT_CONSOLE by default if NEWLIB_LIBC.
Enable stdio to work by default if Newlib is used as libc - it's
reasonable expectation that if full-fledged libc (like Newlib) is
selected, then printf() works out of the box.

Fixes: #5566

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-02-05 07:05:12 -08:00
Vincent Veron
6083939174 lib: add abs function to minimal libc
Add abs function to the minimal libc. This is present in
NEWLIB_LIBC, but adding it here avoid to make a dependency
with NEWLIB_LIBC.

Signed-off-by: Vincent Veron <vincent.veron@st.com>
2018-01-23 10:14:49 -06:00
Andrzej Puzdrowski
9a5a3e06f0 lib: crc: Add crc8-ccitt implementation
This patch adds crc8-ccitt calculation routine.

CRC8 CCITT is required for Flash Circular Buffer
module (originally mynewt module).

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
2018-01-15 15:46:37 -05:00
Sebastian Bøe
15ca6da4c8 cmake: Re-introduce support for CROSS_COMPILE
CROSS_COMPILE is a KBuild feature that was dropped during the CMake
migration. It is now re-introduced. Documentation for it is still
lacking, but at least it now behaves as expected.

Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
2018-01-12 08:37:41 -05:00
Paul Sokolovsky
84e3d3e7e8 libc: Kconfig: Move STDOUT_CONSOLE from subsys/debug
This option is clearly not related to debugging, but affects libc
features.

Fixes: #5566

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-01-11 12:47:10 -05:00
ce3b2edef3 libc: add EILSEQ for use in reporting checksum failure.
This code is commonly used in the Linux kernel for reporting a
retryable error like a failed CRC.  This name and value is already
present in Linux and newlib.

Signed-off-by: Michael Hope <mlhx@google.com>
2018-01-07 14:12:56 -05:00
Anas Nashif
95cd063280 libc: some architectures do not require baremetal libc
When building a native application, we use the host provided libc, so do
not build minimal libc or newlib.

Signed-off-by: Alberto Escolar Piedras <alpi@oticon.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2017-12-27 14:16:08 -05:00
Anas Nashif
e2122cbf89 lib: move ring_buffer from misc/ to lib/
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2017-12-15 20:02:01 -05:00
Anas Nashif
6daf046e8f lib: move crc from drivers to lib/
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2017-12-15 20:02:01 -05:00
Anas Nashif
429c2a4d9d kconfig: fix help syntax and add spaces
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2017-12-13 17:43:28 -06:00
Sebastian Bøe
0829ddfe9a kbuild: Removed KBuild
Signed-off-by: Sebastian Boe <sebastian.boe@nordicsemi.no>
2017-11-08 20:00:22 -05:00
Sebastian Bøe
12f8f76165 Introduce cmake-based rewrite of KBuild
Introducing CMake is an important step in a larger effort to make
Zephyr easy to use for application developers working on different
platforms with different development environment needs.

Simplified, this change retains Kconfig as-is, and replaces all
Makefiles with CMakeLists.txt. The DSL-like Make language that KBuild
offers is replaced by a set of CMake extentions. These extentions have
either provided simple one-to-one translations of KBuild features or
introduced new concepts that replace KBuild concepts.

This is a breaking change for existing test infrastructure and build
scripts that are maintained out-of-tree. But for FW itself, no porting
should be necessary.

For users that just want to continue their work with minimal
disruption the following should suffice:

Install CMake 3.8.2+

Port any out-of-tree Makefiles to CMake.

Learn the absolute minimum about the new command line interface:

$ cd samples/hello_world
$ mkdir build && cd build
$ cmake -DBOARD=nrf52_pca10040 ..

$ cd build
$ make

PR: zephyrproject-rtos#4692
docs: http://docs.zephyrproject.org/getting_started/getting_started.html

Signed-off-by: Sebastian Boe <sebastian.boe@nordicsemi.no>
2017-11-08 20:00:22 -05:00
Ramakrishna Pallala
6ca3cc6fc4 lib: remove TinyCrypt Kconfig menu
Remove the TinyCrypt Kconfig menu as it is moved
under /ext/lib/crypto/

Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
2017-11-06 13:38:58 -05:00
Thiago Macieira
034b458141 libc: Add support for static_assert
The C11 standard requires this. From 7.2 "Diagnostics <assert.h>"
paragraph 1:
> The header <assert.h> defines the assert and static_assert macros...

paragraph 3:
> The macro
>     static_assert
> expands to _Static_assert.

Since static_assert is a keyword in C++11, don't define it if C++.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2017-10-25 10:43:38 -07:00
Thiago Macieira
eb7848a1df libc: Make no-debug assert() conform to C requirements
The C standard requires assert() to be a void result, so you
could write something like:

  return assert(x), x;

From the C11 standard (7.2 Diagnostic <assert.h>):
> If NDEBUG is defined as a macro name at the point in the source file
> where <assert.h> is included, the assert macro is defined simply as
>    #define assert(ignore) ((void)0)

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
2017-10-25 10:43:38 -07:00
Kumar Gala
090a6d4f4e libc: minimal: Add typedefs for fast types
Some of the HALs in ext require fast types to be defined.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-10-19 12:16:11 -05:00
Anas Nashif
9edce87627 lib: json: move json.h to include/
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2017-10-03 14:50:14 -04:00
Anas Nashif
05609bd6fc json: fix doxygen description
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2017-10-03 14:50:14 -04:00
Leandro Pereira
7de019a256 lib: json: Silence warning about unused parameter
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-09-07 05:49:17 -05:00
Leandro Pereira
7a72ecd314 lib: json: Alignment should be considered when calculating struct size
This was causing an unaligned pointer read on some architectures,
leading to crashes.  This could be alternatively solved by rounding
the size to the nearest power of 2, but this wouldn't work with
packed structs.

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-09-07 05:49:17 -05:00
Leandro Pereira
3384262827 lib: json: Fix compilation warnings
This appears to be a bug in GCC: when an anonymous union contains
anonymous structs, GCC issues a warning that a field in one of the
anonymous structs has not been initialized.  Fix by making the
structs not anonymous.

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-09-07 05:49:17 -05:00
Leandro Pereira
d66069d051 lib: json: Fix off-by-one error when encoding objects
append_bytes_to_buf() already writes a NUL byte; no need to call
append_bytes() again with "" and size 1.

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-09-07 05:49:17 -05:00
Aska Wu
1f7a6766d3 newlib: xtensa: Fix a newlib link error
xtensa linker script defines "_heap_sentry" but newlib references
"__heap_sentry".

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-08-10 09:33:13 -05:00
Leandro Pereira
07d8ef008a libc: minimal: Add empty sys/cdefs.h
This header is included by some files provided by ESP-IDF.  Nothing
from this header file is actually used: it's only being added allow
things to compile with the minimal libc.

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-06-21 12:35:49 -04:00
Anas Nashif
397d29db42 linker: move all linker headers to include/linker
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2017-06-18 09:24:04 -05:00
Marti Bolivar
25d62edd01 lib: json: add helper macros for arrays of objects
Since JSON_OBJ_DESCR_ARRAY is suitable only for arrays of primitives,
add JSON_OBJ_DESCR_OBJ_ARRAY (and a ..._NAMED variant), to allow users
to handle arrays of objects.

Having a macro is important, given the unintuitive space optimization
used for storing the offset to the structure element containing the
number of elements in the array.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-08 15:11:23 -04:00
Marti Bolivar
b4c8d83eab lib: json: fix arr_parse()
This function currently fails when decoding an array with number of
elements exactly equal to the maximum available in the struct.

To fix this, move the check for if the current field is past the end
of the array to just before attempting to decode a value. This allows
the last element to be followed by a JSON_TOK_LIST_END token in the
case that the array is full, and the function to return success.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-08 15:11:23 -04:00
Marti Bolivar
2620ae0932 lib: json: fix arr_encode()
The JSON library doesn't properly encode arrays whose elements are of
object type. Fix that.

This fix avoids allocating a temporary descriptor on the stack, and
keeps the size of struct json_obj_descr unchanged, by preserving an
unintuitive size optimization made by the library.  See the comments
in the patch for more details.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-08 15:11:23 -04:00
Marti Bolivar
a4b67de3fd lib: json: clarify JSON_OBJ_DESCR_ARRAY usage
This (and JSON_OBJ_DESCR_ARRAY_NAMED) are really intended for handling
arrays of primitive type only. They don't allow users to declare
descriptors for arrays of objects. Clarify this in the Doxygen.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-08 15:11:23 -04:00
Leandro Pereira
7763a1dee9 lib/json: Fix build warnings (-Wpointer-sign) with LLVM/icx
Move all characters to "char" type: no implicit conversions between
"unsigned char", "u8_t", etc.

Tested with ISSM 2016.2.085.

Jira: ZEP-2159
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-06-05 21:46:04 -04:00
Marti Bolivar
44499768a8 lib: json: add JSON_OBJ_DESCR_*_NAMED variants
The set of valid JSON field names is larger than the set of C
identifiers. This can result in structure field names which pack
decoded JSON values which necessarily differ from the field names in
the JSON.

Support this by adding _NAMED variants to each of the JSON_OBJ_DESCR_*
helper macros. For example, JSON_OBJ_DESCR_PRIM_NAMED allows users to
declare a descriptor field for a primitive type, whose structure field
name is different from the JSON field name.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-05-31 14:53:26 -04:00
Marti Bolivar
31fe26d611 lib: json: fix JSON_OBJ_DESCR_ARRAY Doxygen example
This is missing two required arguments.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-05-31 14:53:26 -04:00
Andrew Boie
614db776dc libc: minimal: fix xtensa build
Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2017-05-19 16:33:16 -04:00
Paul Sokolovsky
8cc6f6ddd6 kernel: errno: Use per-thread accessor function compatible with Newlib
Newlib names this function __errno(), so if we want Zephyr to work
with Newlib seamlessly, it's better to just follow Newlib's naming
convention for Zephyr's own minimal libc.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-05-10 20:54:56 -04:00
Marti Bolivar
3c0eac3cba lib: json: escape strings in-place
Currently, json_escape() allocates a temporary buffer on the stack
that's the size of the string being escaped.

Stack space is precious and longer JSON strings can run into the
hundreds of bytes, so re-implement this routine to escape in place.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-05-08 08:52:52 -04:00
Marti Bolivar
4088530a12 lib: json: add JSON_OBJ_DESCR_PRIM
There are already helper macros for declaring descriptor fields of
object and array type. Add one for primitive types as well.

The fact that the JSON test code defines one proves that it's useful,
so there should be one provided for other users.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-05-03 13:52:57 -04:00
Kumar Gala
9a74ee1730 lib: convert to using newly introduced integer sized types
Convert code to use u{8,16,32,64}_t and s{8,16,32,64}_t instead of C99
integer types.

Jira: ZEP-2051

Change-Id: I74bc6384c4090f4ae322e3aa151874f583a5fe73
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-20 11:56:00 -05:00
Kumar Gala
789081673f Introduce new sized integer typedefs
This is a start to move away from the C99 {u}int{8,16,32,64}_t types to
Zephyr defined u{8,16,32,64}_t and s{8,16,32,64}_t.  This allows Zephyr
to define the sized types in a consistent manor across all the
architectures we support and not conflict with what various compilers
and libc might do with regards to the C99 types.

We introduce <zephyr/types.h> as part of this and have it include
<stdint.h> for now until we transition all the code away from the C99
types.

We go with u{8,16,32,64}_t and s{8,16,32,64}_t as there are some
existing variables defined u8 & u16 as well as to be consistent with
Zephyr naming conventions.

Jira: ZEP-2051

Change-Id: I451fed0623b029d65866622e478225dfab2c0ca8
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-20 16:07:08 +00:00
Kumar Gala
21309ea44b lib: json: use explicit unsigned char to avoid array subscript error
If we use newlib the isdigit (and other similar functions) return an
error as char can possibly be viewed as signed:

usr/include/ctype.h:57:54: error: array subscript has type ‘char’ [-Werror=char-subscripts]
 #define __ctype_lookup(__c) ((__ctype_ptr__+sizeof(""[__c]))[(int)(__c)])

Being explicit about the char being unsigned char deals with this.

Change-Id: If2416218220ef5b29f1a69470cbcc6b4fd49ef86
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-14 19:03:41 +00:00
Leandro Pereira
262365c727 lib: json: Fix parsing of boolean values
Boolean values were being decoded using the descriptor type rather than
the value type.

Jira: ZEP-1607
Change-Id: I0c9324ee705af973ccf738e92785820c3a5fb692
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-03-25 13:11:55 +00:00
Leandro Pereira
4c7803b59f lib: json: Ignore spaces while scanning for tokens
The function to ignore spaces was not being called, so some tokens had
whitespace in the beginning.  They were correctly lexed, but parsing
could eventually fail.

Jira: ZEP-1607
Change-Id: I796596143895fa0fa652641f56af9a03e7a65b7a
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-03-25 13:11:54 +00:00
Leandro Pereira
844ef6744e lib: json: Fix number parsing
Comparing *endptr with '\0' will always be true before replacing
*token->end with prev_end.

Jira: ZEP-1607
Change-Id: I224129586e15380d3919bfba3db4fcf38c28cb07
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-03-25 13:11:53 +00:00
Leandro Pereira
122cdb3956 lib: json: Do not use translation tables to encode characters
It has been suggested in a review to use a simple function with a
switch statement instead of the table trick.

Jira: ZEP-1607
Change-Id: I5290de175021bfa8642334548ece8266d4c137f0
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-03-25 13:11:52 +00:00
Leandro Pereira
95ec49cb70 lib: json: Simplify lexing of "true", "false", and "null" tokens
Roll the loop in an accept_run() function and use it to match "rue" and
"alse" depending on the first character of the token.  Use that to lex
"ull" after finding "n" as well.  This reduces the code slightly.

Jira: ZEP-1607
Change-Id: Iec8ff6ae2fb79e7fe65d476d1574c5943d23e14f
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-03-25 13:11:52 +00:00
Leandro Pereira
b9b1c18cd7 lib: json: Add encoding support
Serializing an object in JSON is quite tricky to do by hand, and with
an array of descriptor structs, there's enough information to do that
programatically.

The encoder takes a callback function, so that one can be written to
write bytes to, for instance, a struct net_buf.  This way, there's no
guesswork to determine the buffer size, reducing the possibility of
overflowing the stack.

Jira: ZEP-1607
Change-Id: I5ccf1012e46c1db32fcfdf2ecee4a1ef44c927d5
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-03-25 13:11:51 +00:00
Leandro Pereira
67ac6f6701 lib: json: Parse nested objects and arrays
Parse arrays and nested objects.

Array parsing is limited to items of the same type, and requires an array
with fixed number of elements.  Elements can be of any type supported by
the parser, including arrays and objects.

The return value of json_obj_parse() won't be that helpful: the nth bit
will only be set if the object has been fully decoded.

Jira: ZEP-1607
Change-Id: I472e402ae3f36a1bd1505decc0313f74cbfa2e07
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-03-25 13:11:50 +00:00
Leandro Pereira
fb192c5da0 lib: Add minimal JSON library
This is a minimal JSON parser (and string encoder helper).  This has
been originally written for the NATS client sample project, but since
it's a generic bit of code, it's also being provided as a library
outside the NATS application source.

It's limited (no support for arrays, nested objects, only integer
numbers, etc), but it is sufficient for the NATS protocol to work.

Jira: ZEP-1012

Change-Id: Ibfe64aa1884e8763576ec5862f77e81b4fd54b69
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-03-09 20:33:45 +02:00
Kumar Gala
91e9f87499 libc: attribute minimal libc printf style functions with __printf_like
Add __printf_like attribute to printf style functions in minimal libc to
enable the compiler checking this provides.  We fixup the associated
issues that are now found by utilizing these checks.

Change-Id: I74ac0d0345782463d9fb454f7161d6b4af211ba5
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-03-02 08:46:30 -06:00
Leandro Pereira
1b8f824975 libc: Add isalnum() to ctype.h
The isalnum() primitive is used by the NATS protocol implementation to
vaildate some of the inputs.

This uses primitives that were already in place.

Change-Id: Ib53eeb7ae002a42f5b6aa8d4fc61baca029a042d
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-02-17 20:39:44 +00:00
Anas Nashif
15a6598691 Merge "Merge remote-tracking branch 'origin/core'" 2017-02-15 04:33:25 +00:00
Sergio Rodriguez
f054d3e897 libc/include: Adding time.h
Dummy time.h to fulfill the compilations requirements of certain
libraries i.e. mbedTLS

Change-Id: I07e66dbf07337b935dabe9eecdf1be3850bbf394
Signed-off-by: Sergio Rodriguez <sergio.sf.rodriguez@intel.com>
2017-02-14 08:30:35 +02:00
Mazen NEIFER
44a98fb5fd Xtensa port: Fixed defintion of MAX_HEAP_SIZE, thus, compilation of new_lib.
Xtensa linker script defines _heap_sentry which helps computing the heap size.

Change-Id: Ic35db3c98aaaebe575370fb5479de1a58e6e441e
Signed-off-by: Mazen NEIFER <mazen@nestwave.com>
2017-02-13 11:39:02 -08:00
Anas Nashif
cebc7f95aa newlib: make sure the chain of includes has generated_dts_board.h
Change-Id: I2021a30e1bc16e3eb9fdcc793b908bd0e610d01d
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2017-02-11 07:00:39 +00:00
Andy Gross
c30b400a16 libc-hooks: Fix include file for arch ARM
This patch adds in the include to get the CONFIG_SRAM definitions on
systems which are using device tree generation.

Change-Id: Ie61efbcdfc900a2c682a2fb8bbaecb61071a20f8
Signed-off-by: Andy Gross <andy.gross@linaro.org>
2017-02-10 23:42:31 +00:00
Andrew Boie
c2a91b1b2e libc: minimal: rename private macro
As it turns out Xtensa SDK headers also define _Restrict, causing
havoc. As this was intended to be a private macro, rename it to something
less likely to cause a collision.

Change-Id: I0a7501a1af8cf87efb096872a91a7b44bd2bbdca
Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2017-02-04 19:22:07 +00:00
Marcus Shawcroft
458e2ed133 libc/minimal: Fix definition of ssize_t
Each GCC target backend is at liberty to define its own SIZE_TYPE. GCC
uses this for various purposes, not lease it drives the machinery that
spits out format specifier diagnostics when format specifiers are
applied to objects with inappropriate type.  GCC exposes the current
definition of SIZE_TYPE via the preprocessor symbol __SIZE_TYPE__.
The GCC build processes also generates various standard library header
files that directyle expose stanard types in a form consistent with
the current configuration of GCC.  Conventionally standard library
build processes (for glibc and newlib) pick up the header files
generated by the GCC build.

In the minimal libc we have no such build process, we don't pick up
the header files that the GCC build process generated.  Instead we
define our own alternative header files and align them with GCC
manually.

The current definition of ssize_t in minimal libc is out of step with
GCC which means that any use of the %z[du] format modifier will issue
a diagnostic.

We replace the open coded architecture detection in minimal libc and
use GCCs __SIZE_TYPE__ directly.

Change-Id: I63b5e17bee4f4ab83d49e492e58efd3bafe76807
Signed-off-by: Marcus Shawcroft <marcus.shawcroft@arm.com>

tests: fs: Fix printf warning when using newlib

Current code uses %ld format specifier to print data of
type ssize_t. This causes type warnings when built with
newlib. The correct format specifier to be used for
ssize_t is %zd.

Change-Id: I02a3c628e3d6e8a36a09cd694220406d8faf1730
Signed-off-by: Ramesh Thomas <ramesh.thomas@intel.com>
2017-02-01 13:57:43 -08:00
David B. Kinder
ac74d8b652 license: Replace Apache boilerplate with SPDX tag
Replace the existing Apache 2.0 boilerplate header with an SPDX tag
throughout the zephyr code tree. This patch was generated via a
script run over the master branch.

Also updated doc/porting/application.rst that had a dependency on
line numbers in a literal include.

Manually updated subsys/logging/sys_log.c that had a malformed
header in the original file.  Also cleanup several cases that already
had a SPDX tag and we either got a duplicate or missed updating.

Jira: ZEP-1457

Change-Id: I6131a1d4ee0e58f5b938300c2d2fc77d2e69572c
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-01-19 03:50:58 +00:00
Anas Nashif
9f418fe944 libc: add support for risc v
Change-Id: Iaaf87ef48057c52478816771836d8d40b2b05554
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2017-01-13 19:57:19 +00:00
Jean-Paul Etienne
716d25e1e2 libc-hooks: added USED_RAM_SIZE and MAX_HEAP_SIZE definitions for riscv32
added USED_RAM_SIZE and MAX_HEAP_SIZE definitions for
SOC_RISCV32_QEMU and SOC_RISCV32_PULPINO.

Otherwise, not passing sanitycheck

Change-Id: Ia32b12e1694dc472e9f7f9eb10c5f2e12e928c3a
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-13 19:56:44 +00:00
Flavio Santes
ac7300611c net: Restructure network protocols
This commit restructures the network protocols. Changes applied are:

- Move lib/iot/ to subsys/net/lib
- Move network protocol headers to include/net
- Move lib/iot/zoap/link-format.h to include/net/zoap_link_format.h
  and link-format.c to zoap_link_format.c
- Move tests/iot/ to tests/net/lib/
- Adapt sample code
- Adapt build system
- Modify doxygen paths

Change-Id: I37085fa4cc76a8a8e19a499ecb4e87b451120349
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
2017-01-02 10:03:19 +01:00
Vinicius Costa Gomes
909ad359bf lib/zoap: Fix incorrect values for payload size
When retrieving the payload length of the payload, there was a mistake,
what was already in the buffer (buf->len) was being considered twice,
net_buf_tailroom() already takes buf->len into account, resulting in
incorrect values.

Change-Id: I3b78d55abca4a448cab5e035b9d66352a3c59110
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2017-01-02 10:03:18 +01:00
Flavio Santes
44b2f212a1 iot/dns: Use a k_sem for the DNS rx routine
This patch introduces a k_sem that will block until:
- data is received, or
- the user-provided timeout expires

This change allows us to simplify our previous DNS client
implementation.

This change is related to ZEP-1357 because we are refactoring the
DNS client API removing the continuous update of the net_context_recv
routine that seems to be causing issues after the kernel's update.

Jira: ZEP-1357

Change-Id: If01c9274ac8f096f0095a2872f86be2e007212ee
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
2016-12-16 19:14:53 +01:00
Flavio Santes
5d0900ea93 iot/dns: Update DNS client private routines
This commit updates some DNS client private routines to simplify
function signatures and reduce stack usage. Updated routines now
use the dns_context structure.

Change-Id: Ic0d347ea9d610e85eed4179aee08955cf2302436
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
2016-12-16 19:14:52 +01:00
Flavio Santes
0d257a009a iot/dns: Introduce the dns_context structure
This commit introduces the dns_context structure.

This new structure will reduce stack overhead due to
the simplication of the dns_resolve routine signature.

Furthermore, the timeout parameter is now int32_t
instead of uint32_t.

The dns sample application is also updated.

Change-Id: I5d789656bacbd23c4654edce5d116a88dc42c354
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
2016-12-16 19:14:52 +01:00
Anas Nashif
fe958df4dd libc: rework libc selection and reduce Kconfigs
Moved all libc Kconfigs to where the code is and remove the default
Kconfig for selecting the minimal libc. Minimal libc is now the default
if nothing else is configured in.

Removed the options for extended libc, this obviously was restricting
features in the minimal libc without a good reason, most of the
functions are available directly when using newlib, so there is no
reason why we need to restrict those in minimal libc.

Jira: ZEP-1440
Change-Id: If0a3adf4314e2ebdf0e139dee3eb4f47ce07aa89
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2016-12-15 22:31:28 +00:00
Johan Hedberg
71c7c01819 net: buf: Remove the need for net_buf_pool_init()
In order to keep the initialization process light-weight, remove
net_buf_pool_init() and instead perform the initialization of the pool
and buffers in a "lazy" manner. This means storing more information
in the pool, and removing any 'const' members from net_buf. Since
there are no more const members in net_buf the buffer array can be
declared with __noinit, which further reduces initialization overhead.

Change-Id: Ia126af101c2727c130651b697dcba99d159a1c76
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2016-12-15 09:58:02 +02:00
Stig Bjørlykke
4ca8ca9b1c iot/mqtt: Fix rlen_decode size check.
If receiving a malformed MQTT packet with less than 5 bytes it's
possible to get a read one byte behind buf.

Change-Id: I34425add57c937c8fd9df5bf7b72af092d6f5f32
Signed-off-by: Stig Bjørlykke <stig@bjorlykke.org>
2016-12-14 22:09:19 +01:00
Johan Hedberg
c3e08c8fea net: buf: Redesigned pool & buffer allocation API
Until now it has been necessary to separately define a k_fifo and
an array of buffers when creating net_buf pools. This has been a bit
of an inconvenience as well as blurred the line of what exactly
constitutes the "pool".

This patch removes the NET_BUF_POOL() macro and replaces it with a
NET_BUF_POOL_DEFINE() macro that internally expands into the buffer
array and new net_buf_pool struct with a given name:

	NET_BUF_POOL_DEFINE(pool_name, ...);

Having a dedicated context struct for the pool has the added benefit
that we can start moving there net_buf members that have the same
value for all buffers from the same pool. The first such member that
gets moved is the destroy callback, thus shrinking net_buf by four
bytes. Another potential candidate is the user_data_size, however
right not that's left out since it would just leave 2 bytes of padding
in net_buf (i.e. not influence its size). Another common value is
buf->size, however that one is also used by net_buf_simple and can
therefore not be moved.

This patch also splits getting buffers from a FIFO and allocating a
new buffer from a pool into two separate APIs: net_buf_get and
net_buf_alloc, thus simplifying the APIs and their usage. There is no
separate 'reserve_head' parameter anymore when allocating, rather the
user is expected to call net_buf_reserve() afterwards if something
else than 0 headroom is desired.

Change-Id: Id91b1e5c2be2deb1274dde47f5edebfe29af383a
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2016-12-13 21:41:53 +00:00
Flavio Santes
b965fb5ff1 iot: Add HTTP support for Zephyr
This commit adds HTTP message handling support for Zephyr.
So, no network routines are involved at this level.

To add HTTP message handling support for Zephyr, we explored the
following options:

1. Importing an external project and perhaps adapting it to fit our
   requirements.
   The criteria to pick one codebase among all the available projects
   are: licensing, correctness and performance.

2. Writing our own implementation from scratch.

We decided to import an external project instead of implementing our
own parser, mainly due to code maturity and correctness. It could take
more time to obtain a production-ready parser from scratch than adapting
a state-of-art library.

The following is a list of some projects offering similar functionality.

lighttpd (many files)
	* License: revised BSD license
	* Supported: active
	* Comments: this parser can't be integrated to Zephyr due to
	  dependencies that currently are not satisficed by our SDK.

nginx (src/http/ngx_http_parse.c)
	* License: 2-clause BSD-like
	* Supported: very active
	* Comments: this parser can't be integrated to Zephyr due to
	  dependencies that currently are not satisficed by our SDK.

wget (src/http-parse.c)
	* License: GPL 3.0
	* Supported: very active
	* Comments: this code can't be included in Zephyr due to
	  licensing issues

curl (lib/http.c)
	* License: MIT/X derivate, see:
	  https://curl.haxx.se/docs/copyright.html
	* Supported: very active
	* Comment: it must be forked and adapted to run in Zephyr.
	  It is not optimized for low-power devices.

nodejs http-parser (http_parser.c)
	* License: nginx license (2-clause BSD-like) and MIT license
	* Supported: very active
	* Comments: optimized with performance in mind.
	  From https://github.com/nodejs/http-parser: "It does
	  not make any syscalls nor allocations, it does not buffer data,
	  it can be interrupted at anytime. It only requires about 40
	  bytes of data per message stream."

So, nodejs/http-parser looks a very good choice for Zephyr. In this
commit, we integrate nodejs' parser to Zephyr.

Origin: https://github.com/nodejs/http-parser/releases/tag/v2.7.1
        https://github.com/nodejs/http-parser/archive/v2.7.1.tar.gz

NOTE:
	This patch reformats the http_parser files to reduce checkpatch
	warnings. Changes made in this refactoring are available at:
Repo:	https://gitlab.com/santes/http_parser/commits/refactoring1
Commit:	9ccfaa23f1c8438855211fa902ec8e7236b702b1

Jira: ZEP-346
Jira: ZEP-776

Change-Id: I29b1d47f323a5841cd4d0a2afbc2cc83a0f576f0
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
2016-12-02 12:41:20 +02:00
Vinicius Costa Gomes
b1749afbc2 iot/zoap: Increase the range of timeouts
To avoid the risk of overflowing when dealing with retransmission
timeouts, increase the size of their representation to 32-bits.

Change-Id: I7c9c1e00c41f5ba75e19b0fd4527f273852eb85f
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-12-02 12:41:12 +02:00
Flavio Santes
d37b9fa9b5 iot/mqtt: Add the MQTT high-level API
This commit adds the MQTT high-level API with Quality-of-Service
support. The following MQTT messages are covered by this commit:

CONNECT (tx), DISCONNECT (tx), PUBACK (tx, rx), PUBCOMP (tx, rx),
PUBREC (tx, rx), PUBREL (tx, rx), PUBLISH (tx), PINGREQ (tx),
SUBSCRIBE (tx), UNSUBSCRIBE (tx), CONNACK (rx), PINGRESP (rx),
SUBACK (rx) and UNSUBACK (rx).

Where 'tx' stands for transmission: routines that create and send
messages. 'rx' stands for reception: routines that receive an RX
buffer from the IP stack and parse the MQTT mesage contained in
that buffer.

Jira: ZEP-365
Jira: ZEP-591
Jira: ZEP-856

Change-Id: Ibee701a298127eb713aa3fde5aaf7d089ecd1b9d
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
2016-12-02 12:41:09 +02:00
Vinicius Costa Gomes
30dec49fd8 iot/zoap: Remove unnecessary cflags
These cflags were only needed when using the old network stack.

Change-Id: I660e397b2648137450c45d4f2edc8ec6ec7ae774
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-12-02 12:41:05 +02:00
Vinicius Costa Gomes
ac152ea884 iot/zoap: Add support for RFC6690 link format
RFC6690[1] defines a lightweight format for listing and querying
resources and their relationships.

The RFC defines an '.well-known/core' resource that will list the
resources and their associated metadata. It also allows resources to
filtered by their attributes.

The implementation uses the fact that resources are organized in an
array: only the resources present in the array after the
"ZOAP_WELL_KNOWN_CORE_RESOURCE" will be considered, this allows a
primitive form of visibility control for the attributes.

[1] https://tools.ietf.org/html/rfc6690

Change-Id: I2bc21ddf45f20e1f749d8ac36d247474fdaa9d64
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-12-02 12:41:05 +02:00
Vinicius Costa Gomes
ec184107be zoap: Add support for retrieving the underlying net_buf
Add an alternative to zoap_packet_get_payload(), that instead of
returning a byte array, returns the net_buf (with the COAP_MARKER
added, if needed) associated with the packet, positioned so the
application can add more data.

Change-Id: I7c955ef42f5ef8406d77da994d1673e6a69b0b6b
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-12-02 12:41:05 +02:00
Flavio Santes
699083140e iot: Add MQTT v3.1.1 packet handling support for Zephyr
This commit adds support for the MQTT protocol v3.1.1.
Specifically, this commit allows a Zephyr application to create
the following MQTT messages:

CONNACK, CONNECT, PUBLISH, PUBACK, PUBREC, PUBREL, PUBCOMP, UNSUBACK,
SUBSCRIBE, SUBACK, UNSUBSCRIBE, PINGREQ, PINGRESP, and DISCONNECT.

Furthermore, the following messages can be parsed by the routines
provided by this commit:

CONNACK, CONNECT, PUBLISH, PUBACK, PUBREC, PUBREL, PUBCOMP, UNSUBACK,
SUBSCRIBE, SUBACK, PINGREQ, PINGRESP and DISCONNECT.

NOTE: client behavior (routines with network access) and QoS will be
integrated in future patches.

The MQTT v3.1.1 specification can be found at:

  http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html

Origin: Original

Jira: ZEP-365
Jira: ZEP-591
Jira: ZEP-856

Change-Id: Ie0c179370cea22f7554564692bc426a8d5c419d2
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
2016-12-02 12:41:03 +02:00
Ravi kumar Veeramally
c689fd6f1a net: Rename net_nbuf_write to net_nbuf_append
Current net_nbuf_write() api just appends data to last fragment. And
doesn't write data based on offset. That's why renaming this api.

New net_nbuf_write() apis based on offset will be coming soon.

Change-Id: Ie8e13e5f6091a279b62b6d8b0b3928a5187e75b0
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2016-12-02 12:40:51 +02:00
Flavio Santes
8b0467a967 iot/dns: Add '+' operator
This commit adds the '+' operator that keeps symmetry with the line
below.

Change-Id: Icb2a5f22b5202597e3e0cc252436b422ce7b44a2
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
2016-12-02 12:40:51 +02:00
Vinicius Costa Gomes
ea28e4fccb iot/zoap: Add helper for generating tokens
Most applications will want to use randomly generated tokens, add a
helper for that.

Change-Id: If2a6b1d96596024afd2d2ce8e3632900adfe9c0f
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-12-02 12:40:50 +02:00
Vinicius Costa Gomes
dc45748d50 iot/zoap: Port to the native stack
This ports zoap to the native stack.

Just for reference, and totally not scientific, here are the numbers
using the old stack:

$ size outdir/qemu_x86/zephyr.elf
   text	   data	    bss	    dec	    hex	filename
  34657	  10316	  16916	  61889	   f1c1	outdir/qemu_x86/zephyr.elf

With yaip:

$ size outdir/qemu_x86/zephyr.elf
   text	   data	    bss	    dec	    hex	filename
  30575	   9148	   6164	  45887	   b33f	outdir/qemu_x86/zephyr.elf

Jira: ZEP-818

Change-Id: I7992a3e2af7d419081ee5a64d7cc2d49fb628ead
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-12-02 12:40:50 +02:00
Vinicius Costa Gomes
f996299754 iot/zoap: Add support for error 4.15
The "Unsupported Content-Format" error was missing from the list of
supported errors.

Change-Id: I208d79f8949838187b877eaa0a53597d8a5bc6cb
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-12-02 12:40:50 +02:00
Vinicius Costa Gomes
fa5481b1cb zoap: Fix alignment of multiline function arguments
Change-Id: I0a96c87db84671be1790fda34083e7cd53e00cd0
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-12-02 12:40:50 +02:00
Flavio Santes
36bbd7a935 iot/dns: Add DNS Client support for Zephyr
This commit adds support the DNS client API on top of the new
native IP stack. Some features of this implementation are:

- Support for IPv4 and IPv6
- Support for multiple concurrent queries. A net_buf structure is
  required per context. See the DNS_RESOLVER_ADDITIONAL_BUF_CTR
  configuration variable

Origin: Original

Jira: ZEP-793
Jira: ZEP-855
Jira: ZEP-975

Change-Id: I351a636462a1b78a412c9bce1ef3cd0fa6223a52
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
2016-12-02 12:40:49 +02:00
Inaky Perez-Gonzalez
b53e6d7774 libc/minimal: snprintf(): KILL negative len parameter
snprintf() implements the ability to foce a negative value through the
(unsigned) size_t len parameter to allow the formatter to use a
maximum size string.

This is point less, we don't have as much memory and this is a recipe
for all kinds of vulnerabilities.

Kill the whole thing, the testcase it represents and thank Coverity
for finding this thing. Whatever use it had before, it has no more.

Change-Id: If422246548664699d8aa328a1b9304ef13cab7ea
Coverity-ID: 131625
Coverity-ID: 131626
Signed-off-by: Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
2016-11-28 20:49:37 +00:00
Inaky Perez-Gonzalez
11a52750d3 libc: remove useless code in _prf()
Coverity reported a block of deadcode in _prf() that seems to be a
leftover carcass from a previous time. Replaced with a comment in case
someone decides it was needed back.

Change-Id: Id97e84f3279f807e6188371f27f6af157e6d5038
Coverity-ID: 131631
Signed-off-by: Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
2016-11-23 00:57:48 +00:00
Vinicius Costa Gomes
777d57dbfb iot/zoap: Fix decoding of 16-bit delta
When an option code or length representation is encoded in a 16-bit
value, the access was wrong.

Coverity-CID: 151963

Change-Id: Ie7741998cbde348ccf490a6686e68a1ace99920e
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-11-18 23:13:08 +00:00
Inaky Perez-Gonzalez
c41d88fd11 libc: atoi() avoid fallthroughs in switch cases
This is a tentative fix for CID 93807, a valid Coverity warning;
however the code is valid too.

We can choose to silence the warning or rewrite the code in a way that
makes it more verbose but keeps Coverity happy.

Coverity complains about doing an implicit fallthrough in switch case
statement. I prefer patching the code to make it explicit, as the
compiler will optimize out, to avoid having to constantly filter out
the checker's warnings.

Coverity-CID: 93807
Change-Id: I7be334d48567bf52fc2b21de043310e0f73b72db
Signed-off-by: Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
2016-11-16 22:05:53 +00:00
Juro Bystricky
f3bf5abd52 newlib: add support for nano-formatted-io
If newlib is configured with --enable-newlib-nano-formatted-io,
floating-point support is split out of the formatted I/O code into
weak functions which are not linked by default. This leads to a smaller
code by about 16~20k when using newlib "printf" and/or "sscanf" but not
using floating point I/O.

Programs that need floating-point I/O support must explicitly request
linking of one or both of the floating-point functions:

    _printf_float or _scanf_float.

This can be done at link time using the -u option which can be passed
to either gcc or ld.

Implemented via new configuration options:
    CONFIG_NEWLIB_LIBC_FLOAT_PRINTF
    CONFIG_NEWLIB_LIBC_FLOAT_SCANF

Change-Id: I57f9d9f02e6d21d6011d14de7153b1d3ba6f6e32
Signed-off-by: Juro Bystricky <juro.bystricky@intel.com>
2016-11-09 02:47:43 +00:00
Jaakko Hannikainen
24a2fb19f9 lib: Add implementation for strrchr
Signed-off-by: Jaakko Hannikainen <jaakko.hannikainen@intel.com>
Change-Id: I57c549fae0fa8b2321794e9256da63b0a2fe6eaf
2016-11-04 16:53:50 +00:00
Vinicius Costa Gomes
b7833b9851 iot/zoap: Add support for block sized transfers
This will add basic support for sending bodies of data that exceed the
size of a single UDP packet.

Block-wise transfers are defined in the recently adopted RFC 7959[1].

The RFC defines four options, two negotiating the block transfer, and
two for informing the size of the transfer. Depending whether the packet
is a request or a response, each option is defined as
"control" (informative) or descriptive (describes what's in the
payload).

Change-Id: Ic71275558c4afed0298d20e8712f76d53904f89f
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-10-25 12:56:51 +00:00
Vinicius Costa Gomes
75d7147fa4 iot/zoap: Fix comparing pointers of different signedness
strncmp() expects pointers of type 'char *', we could cast option.value
to 'char *', but we can use memcmp() instead, which has the benefit of
being simpler.

Change-Id: I8c4ee4401712f3617c134d8e5b6d84e8f5cc4e1d
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-10-04 12:29:07 +00:00
Anas Nashif
c83e6a1f48 libc: define EWOULDBLOCK to be the same as EAGAIN
EWOULDBLOCK is an old error code from BSD. Not much uses it, and on basically
all modern systems it's defined to the same value as EAGAIN, which is the
System V value for the same condition.

Jira: ZEP-982
Change-Id: I5435ce55fa47f5bd2fac5d881b5b195b025f48a2
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2016-10-03 02:29:42 +00:00
Vinicius Costa Gomes
7079a18267 iot/zoap: Add helpers for dealing with integer options
A good number of option values represent numbers, so add these helpers
so applications can avoid repeating this.

Change-Id: Ied2a844c51808dcafe751a77492d00f2063de7d6
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-10-01 01:11:25 +00:00
Vinicius Costa Gomes
441d22a371 iot/zoap: Add port information to network addresses
uIP keeps the port separated from the IP addresses, so if the
application wants to communicate with a remote endpoint we must also
have the port information available.

Change-Id: I8e2b01fe5717166e1f9cebcc74b2056325b8ccc3
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-10-01 01:11:25 +00:00
Vinicius Costa Gomes
92a79f7351 iot/zoap: Add support for observing resources
This adds support for observing resources, for client and server side.

For the server side we augment the resource struct so it can hold
information about each interested observer.

For client side, observing a resource abstracted as an reply that
can be called multiple times.

Change-Id: If3f0b41e302cff357ab891e6e91ec2d41579fb92
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-10-01 01:11:24 +00:00
Vinicius Costa Gomes
7d0a1279b6 iot/zoap: Fix retrieving the token for every reply
As there is only one response token, we can do this only once at the
beginning of the function, instead of every iteration.

Change-Id: Ibb324a1189929227bd1eb9837c5d330ff8c8dac2
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-09-22 00:54:36 +00:00
Vinicius Costa Gomes
eab5adb282 iot/zoap: Fix subtly wrong indentation
There were a few lines where the alignment was wrong.

Change-Id: I474976c218ac28564dfe9dcda0e0687ed110834e
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-09-22 00:54:35 +00:00
Qiu Peiyang
c53fe299c0 libc: replace null.h and size_t.h with stddef.h
According to commit b71a8a4591 and Jira: ZEP-733
(libc: remove stddef.h which is provided by the compiler),
the stddef.h in zephyr code is removed and it's provided
by the compiler. The original stddef.h includes two head
files, null.h and size_t.h, which are also useless now.
So remove these two deprecated files, in case conflicting
definition.

Change-Id: Ie7163fdbd23c32759425b50f3deff2a57cc051a9
Signed-off-by: Qiu Peiyang <peiyangx.qiu@intel.com>
2016-09-13 10:21:10 +08:00
Vinicius Costa Gomes
2baa577b49 lib: Introduce the CoAP implementation for Zephyr
This is a CoAP implementation tailored for Zephyr's requirements, it
tries to use memory predictably by exposing its control structures to
the user (so the user only pays for the features that are used) and
having a lightweight packet representation (the trade-off is that
adding options is not as efficient as it could be, if there were more
information available).

Change-Id: I6f74146c4626a0c554f50b42f163a076e82805ba
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
2016-09-12 12:25:24 +00:00
Andy Ross
5729235b57 libc/printf: Use compiler-provided 64 bit math, phase 2
Remove the function wrappers around the 64 bit math and just use C
syntax natively, combining ops where appropriate (e.g. there was a
sequence implementing "(x<<2+x)<<2" to do "multiply by 10").  The
_ldiv5 and _rlrshift routines are non-standard ops that provide useful
abstraction, so they remain as separate functions.

Change-Id: I4d83847348fdd7be09887b833c8ccbd2aa1e4182
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2016-09-10 00:49:01 +00:00
Andy Ross
d06eea4eda libc/printf: Use compiler-provided 64 bit math, phase 1
The _to_float() implementation had a somewhat kludgey hand-written 64
bit math implementation, which is unhelpful on Zephyr as all our
toolchains provide a working uint64_t runtime.  This is at best just
dupicated code from libgcc, and at worst less efficient.

This patch replaces the existing 64 bit minilibrary but keeps the
uint32_t[2] API as is for ease of validation and review.

One exception is _ldiv5, a specialized divide-by-five implementation.
The 64 bit division routines are large on some architectures (ARM and
ARC in particular), not pulled in by a default Zephyr build, and will
swamp the benefit from this patch.  So this includes a
refactored/improved _ldiv5 which leverages libgcc for multiword shifts
instead of just using raw division.

Note also the "noinline" attribute on _ladd().  This is a workaround
for an apparent compiler bug when built with -Og or -Os (hand-hacking
the Makefiles to build with -O0 works), perhaps due to my aliasing the
int array with a long long.  This will go away in phase 2.

Change-Id: I63e8c82dabe2bfaa75b63ddb59e5f11d51be538e
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2016-09-10 00:49:00 +00:00
Andy Ross
073cfddd0f libc/printf: Remove vestigial "full" parameter to _to_float()
The _to_float routine was apparently written to be able to take a 32
bit float bit representationa as well as a 64 bit double.  But in a
printf routine, that can never happen per the C standard (where floats
are always promoted to doubles in varargs context).

This was just hard-configured to 1 at the top of the file, and nothing
else in the project sets "DOUBLE" to try to change it.  Just remove
it.  If we ever want code to convert a float to a double in memory so
we can use this routine, we have it in libgcc.  Or even in hardware on
the FPU where available.

Change-Id: I796814c0fce3ce96faa34fde8da411a28c826699
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2016-09-10 00:49:00 +00:00
Andy Ross
a4537cb0d3 libc/printf: Unify & simplify number printing
Hex, octal and decimal all had separately implemented reduction loops
to generate strings.  With only a little work these can all be unified
to a single implementation that works with an arbitrary base.

Performance is probably a little lower owing to the fact that
hex/octal now requires a division per character, and the extra
"reverse the string" trick at the end of the conversion.  But code
size savings are substantial.

Change-Id: I11ff376aeca1483f974d328271e19918221b2a41
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2016-09-10 00:48:59 +00:00
Anas Nashif
b71a8a4591 libc: remove stddef.h which is provided by the compiler
Jira: ZEP-733
Change-Id: I26ba720377826bfa662c61e46c0358f05218524b
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2016-09-09 11:57:41 +00:00
Johan Hedberg
65a8e3c1c9 libc: printf: Add support for 'z' length specifier
The 'z' length specifier is the appropriate one to be used with size_t
(%zu) and ssize_t (%zd) types. Having support for this in our libc
means that we can utilize the compiler format string checks
(__printf_like) without getting warnings of incorrect format
specifiers for size_t and ssize_t variables.

Change-Id: I73fec0145692e0a59934cab548caf24c1c16a3df
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2016-09-07 15:10:57 +00:00
Andrew Boie
ad9d5265c3 libc: minimal: add reduced inttypes.h
This implements a subset of the standard inttypes.h, based on what
other functionality exists in minimal libc.

Change-Id: Ib5685a6da13768ee46acbfca734d145f7018b9e0
Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2016-08-28 11:33:15 +00:00
Carles Cufi
1980b7bb0f lib: Use offsetof() builtin with GCC
The default offsetof() implementation generates a warning
(variably modified <variable> at file scope) when used in
to define the size of an array. By using this builtin with
GCC we avoid the warning and make sure no variable-size
arrays are used.

Change-Id: Iae215f777241f7daaa061067230086dffaa8311d
Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2016-08-27 18:29:27 +00:00
Maureen Helm
0a9e0d0a5e libc: Add assert.h to minimal libc
This is an adaptation of
net/ip/tinydtls/platform-specific/config-zephyr.h to allow external
imports such as KSDK to include assert.h.

Change-Id: I0afee37deb79447363037ba6b4bb9cdbc629be3a
Signed-off-by: Maureen Helm <maureen.helm@nxp.com>
2016-07-21 02:09:44 +00:00
Sergio Rodriguez
6d5981bf28 lib: Adding the strstr() function
The strstr function finds the first occurrence of a substring
in another string, null terminated strings are not compared, this
function is added for compability for porting other libraries (like
mbedtls)

Jira: ZEP-327

Origin: http://www.leidinger.net/freebsd/dox/libkern/html/d3/d29/
strstr_8c_source.html

Change-Id: I52aac218ce0bd86373ec60f5afc49a92c85f6319
Signed-off-by: Sergio Rodriguez <sergio.sf.rodriguez@intel.com>
2016-07-19 22:35:21 +00:00
Andrew Boie
00365c188c nios2: Kconfig: remove some unused config options
We get these out of system.h instead. A clause in libc-hooks.c
for newlib added since we don't get RAM size from
CONFIG_SRAM_BASE_ADDRESS.

Change-Id: Ic35113395b951f625e8e29658afe19c525037964
Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2016-06-10 15:22:42 +00:00
Kumar Gala
7fdd4f758d newlib: Support both namespace protected & nonprotected stubs
Some variants of newlib build and expect namespace protected stubs
(typically having an underscore prefix).  To support such newlib variants
change all the stubs to the namespace protect version and use function
aliases to support the nonprotected namespace version.

Change-Id: I6a4162eca949afec96b152ffe6f60b87c4496c4d
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2016-06-09 16:22:28 +00:00
Kumar Gala
ab5614ed9e newlib: Drop _fstat implementation
We only need to implement fstat(), so drop the _fstat implementation and
have fstat() match the newlib docs for minimal implementation and only
set st_mode = S_IFCHR.

Change-Id: Iba9042707d1ee5975ab98293cfe20e2996b17c05
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2016-06-09 16:22:27 +00:00
Anas Nashif
7d76e9619b ext: move tinycrypt to ext/lib
Tinycrypt is maintained at github and thus should be treated as an
external library and hosted under ext/.

Change-Id: I4c4a3bcdacf01d4922919e5ea1f9dec21a19cd37
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2016-06-01 14:27:41 -04:00
Szymon Janc
f23582fe71 tinycrypt: Fix discrepancy of ecc_make_key definition and declaration
ecc_make_key declaration has random size of NUM_ECC_DIGITS * 2 but
definition has (and use) only NUM_ECC_DIGITS.

Change-Id: I18f0d7992b21a2ed7ed99851b1b795cff0a08a10
Signed-off-by: Szymon Janc <ext.szymon.janc@tieto.com>
2016-05-27 11:19:00 +00:00
Anas Nashif
37b1d1eb55 ext: include headers based on HAL being used
For CMSIS we now have HAS_CMSIS which needs to be added to the
SoC definition.
Instead of changing the main Makefile we now include a sub-Makefile
with all related header and library paths that are hosted in
ext/

Move redifintion of LIB_INCLUDE_DIR later to get variables defined
in Makefile.toolchain.*

Change-Id: I9f90f90247c2a66b4565427b89d4e1d4dd5c9622
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2016-05-27 01:38:23 +00:00
Kumar Gala
9ec2f3be80 Cleanup whitespace in Kconfig files
Convert leading whitespace into tabs in Kconfig files.  Also replaced
double spaces between config and <prompt>.

Change-Id: I341c718ecf4143529b477c239bbde88e18f37062
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2016-05-25 13:28:07 -05:00
Anas Nashif
083a7ad0df build: move library related code to lib/
Cleanup main Makefile and remove all library related code to
lib/ to better support the increasing number of libraries and
to keep the main Makefile clean from library dependencies.

Jira: ZEP-308
Change-Id: Id83cf309020604b8eab8d80cad0021905d5b5f7a
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2016-05-16 19:43:50 +00:00
Anas Nashif
0b2c44a771 tinycrypt: move from lib to regular objects
tinycrypt was built using the lib- scripts without any real benefit. We
also had a wrong placement of the Kconfig files under misc/ and a Kconfig
file for Crypto that was never used before.

Change-Id: I82d5902d92e7c06e10a95f418d9ead3cbcabcce4
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2016-05-16 19:43:50 +00:00
Andrew Boie
3395211a0b newlib: treat x86 as the special case
Although it's unclear why x86 has a different naming convention,
this scales better.

Change-Id: I939b9d4d04b1833391304700a7c12c9c8607192f
Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2016-05-09 18:09:29 +00:00
Bogdan Davidoaia
c2216c7088 libc: newlib: fix RAM config names for ARC
The RAM config symbols need to be updated as they were named by the
commit:
	1a1f7fd arc: make SRAM/DCCM values configurable

Change-Id: Ieeb06de2f77b4c9e10a0bc32d8318834ce150f5e
Signed-off-by: Bogdan Davidoaia <bogdan.m.davidoaia@intel.com>
2016-05-05 14:08:50 +00:00
Andrew Boie
9433895954 nios2: basic build, non-functional
Basic build framework for Nios2. Everything is stubbed out,
we just want to have a build going so that we can start to
parallelize implementation tasks.

This patch is not intended to be functional, but should be
able to produce a binary for all the nanokernel-based
sanity checks.

Change-Id: I12dd8ca4a2273f7662bee46175822c9bbd99202a
Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2016-05-03 23:18:45 +00:00
Benjamin Walsh
f557d71b52 libc: add strncat() to minimal libc
Change-Id: I0a44e1bb80c815ff89d8c9648d6e5db70e911fd2
Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-04-16 05:29:21 +00:00
Peter Mitsis
7f14e706ec printf: Limit width modifier to [0..MAXFLD]
When capping the the absolute value of the width modifier in printf(),
it must first be cast to an 'unsigned'. This stems from the fact that
in two's complement, not all negative numbers have a positive counterpart.

Change-Id: I3e6f92f68ab1b8dab48bbf883c5ad4b078a93f87
Signed-off-by: Peter Mitsis <peter.mitsis@windriver.com>
2016-03-19 11:03:54 +00:00
Johan Hedberg
41f8a33555 libc: Add off_t definition
Add definition for the POSIX standard off_t type.

Change-Id: I1142428a3d226d641a8628cbba71cb3ea341ef92
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2016-03-11 01:09:56 +00:00
Johan Hedberg
6692e98298 libc: Move ssize_t definition to sys/types.h
There's no need to have a separate file for this, and we'll be adding
other types like off_t here soon as well.

Change-Id: I40629a5a0fba7af44828eaeead294e4e55844bb0
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2016-03-11 01:09:56 +00:00
Iván Briano
4370b5da6c libc-hooks: Provide the 'open()' syscall
Some parts of Newlib, notably the locale handling stuff, will try to
call open(), and if it's not provided, any application that for some
reason is bringing in that code will fail to link.
Having a non-working implementation keeps that code working.

Change-Id: I28345dabb93431d6b80c839b23a46b7f99dc8734
Signed-off-by: Iván Briano <ivan.briano@intel.com>
2016-03-10 23:28:15 +00:00
Jithu Joseph
4ad7f2a5a3 libc-hooks: newlib's heap may use entire unused RAM
Use linker symbol and board configs to determine the start
and extent of remaining RAM present in a board and use
it as newlib's heap.

Change-Id: I7128cf2857664331d83f212f27e8af7ad3bb8936
Signed-off-by: Jithu Joseph <jithu.joseph@intel.com>
2016-02-27 09:57:19 +00:00
Iván Briano
428d51b5db libc-hooks: Make newlib's heap size configurable
While we don't have a proper heap that can make use of all the
available RAM, make it possible for applications using Newlib to
configure the size of the heap exposed via the sbrk() hook.

Change-Id: I4e3193c1f2df0ace1dbc5b1f6ceb2cdc61479762
Signed-off-by: Iván Briano <ivan.briano@intel.com>
2016-02-20 14:57:53 +00:00
Johan Hedberg
da1552a923 libc: Add definition for ssize_t
Add definition for ssize_t that may be useful to represent sizes with
variables that also need to be able to have negative values (e.g. to
represent errors).

Change-Id: I52ec69591ccfd760021dad38f5ba1b1ebe707ea5
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2016-02-20 14:15:26 +00:00
Peter Mitsis
8413e1f872 doc: Fix misuse of @file doxygen tag
Summary lines follow the @brief tag instead of the @file tag.

This prevents doxygen build warnings such as ...

    The name xxx supplied as the second argument in the \file
    statement is not an input file

Change-Id: I1014586ad21be12e14aa1d2a942e6b8a11211795
Signed-off-by: Peter Mitsis <peter.mitsis@windriver.com>
2016-02-11 20:11:03 +00:00
Szymon Janc
c12c48c4b2 tinycrypt: Fix compilation with ECC enabled
Common ecc.c code was not compiled resulting in following link error:

crypto/tinycrypt/lib.a(ecc_dh.o): In function `ecc_make_key':
crypto/tinycrypt/source/ecc_dh.c:50: undefined reference to `vli_set'
crypto/tinycrypt/source/ecc_dh.c:51: undefined reference to `curve_n'
crypto/tinycrypt/source/ecc_dh.c:51: undefined reference to `vli_sub'
crypto/tinycrypt/source/ecc_dh.c:54: undefined reference to `curve_n'
crypto/tinycrypt/source/ecc_dh.c:54: undefined reference to `vli_cmp'
crypto/tinycrypt/source/ecc_dh.c:53: undefined reference to `vli_cond_set'
crypto/tinycrypt/source/ecc_dh.c:56: undefined reference to `vli_isZero'
crypto/tinycrypt/source/ecc_dh.c:62: undefined reference to `curve_G'
...

Change-Id: Ib590fd7330753c0ae8ef54159b0f2b7461b5b38e
Signed-off-by: Szymon Janc <ext.szymon.janc@tieto.com>
2016-02-10 22:14:58 +00:00
Szymon Janc
0f7bc9dd67 tinycrypt: Fix few typos in Kconfig help messages
Change-Id: Ia6442359e1ab5116083c065ae63e4b929e74ef76
Signed-off-by: Szymon Janc <ext.szymon.janc@tieto.com>
2016-02-10 22:14:58 +00:00
Peter Mitsis
08b353b8b4 minimal libc: stdlib.h no longer includes ctype.h
The libc 'ctype.h' header file is not supposed to be automatically
included when including 'stdlib.h'.

Change-Id: I821a9946894572a7b82804b1569b7b18c24316db
Signed-off-by: Peter Mitsis <peter.mitsis@windriver.com>
2016-02-09 23:41:59 +00:00
Dan Kalowsky
a97c9bce4a lib : tinycrypt : Adding README for external library
Adding in a README file for the external library of TinyCrypt providing
details for origin, commit id, and where to find the original.

Source: Original to Zephyr Project

Change-Id: Ibc7ec33f7f459e8df88978b5b4505e67a2141998
Signed-off-by: Dan Kalowsky <daniel.kalowsky@intel.com>
2016-02-08 20:09:04 -05:00
Constanza Heath
eaed145cb6 Integration of TinyCrypt v2: ECC, CMAC, AES-CCM with test cases for CMAC and CCM.
Change-Id: I26a5c3027af0d00f55021c1ac063100606084314
Signed-off-by: Constanza Heath <constanza.m.heath@intel.com>
2016-02-05 20:25:23 -05:00
Peter Mitsis
df3c6b13ab c++: Update minimal libc headers
Updates the minimal libc headers for differences between the C and C++
languages.  This includes ...
   1. Conditionally defining "bool", "true" and "false" as they are
      already keywords in C++.
   2. Making the definition of NULL language dependent.
   3. Using the _Restrict macro instead of the restrict keyword as
      restrict exists in C, but not in C++.
   4. Changing the definition of size_t so that it is compatible with
      what the compiler expects when building the new operator stubs
      (as it varies by architecture).

Change-Id: I37ff058a60b90a05f96e9dd6f61d454d143041ce
Signed-off-by: Peter Mitsis <peter.mitsis@windriver.com>
2016-02-05 20:25:22 -05:00
Peter Mitsis
497d0e53c4 libc: Consolidate size_t definitions
Consolidates the definitions of size_t in the minimal libc library.
This prevents code duplication.

Change-Id: I996990ad9093ebec1f9ba0045d14319e1a243e70
Signed-off-by: Peter Mitsis <peter.mitsis@windriver.com>
2016-02-05 20:25:22 -05:00
Peter Mitsis
b111d6c932 libc: Consolidate NULL definitions
Consolidates definitions of NULL in the minimal libc library.
This prevents code duplication.

Change-Id: Ia4a2bde1329d66e7c83afeab806a39b22704ab48
Signed-off-by: Peter Mitsis <peter.mitsis@windriver.com>
2016-02-05 20:25:22 -05:00
Peter Mitsis
a0e4568760 c++: Add extern "C" { } block to header files
Adds extern "C" { } blocks to header files so that they can be
safely used by C++ source files.

Change-Id: Ia4db0c36a5dac5d3de351184a297d2af0df64532
Signed-off-by: Peter Mitsis <peter.mitsis@windriver.com>
2016-02-05 20:25:22 -05:00
Andre Guedes
aa47bd8d6c newlib: Rename exit to _exit
This patch renames the 'exit' function from newlib/libc-hooks.c to
'_exit' since this is the correct name of the newlib system call.

This patch fixes some linking errors that occur when an applications
uses abort() or assert().

Change-Id: I593e5ec5dc0f84dbbebe4aceb38703256e140914
Signed-off-by: Andre Guedes <andre.guedes@intel.com>
2016-02-05 20:25:16 -05:00
Vlad Lungu
8de1dac833 Add isgraph()/isprint()/isxdigit()/toupper()
Needed for porting various software

Change-Id: Ic5e89f4e606e46bd0c6de67cebb5b8e3159ab90e
Signed-off-by: Vlad Lungu <vlad.lungu@windriver.com>
2016-02-05 20:25:06 -05:00
Johan Hedberg
26484222ef lib/libc: Add implementation for isprint()
Add a simple implementation of isprint() which is used to test for a
printable character.

Change-Id: I0ebca10ec0f8e43ee245f6508c5b82c6e207bd32
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2016-02-05 20:25:03 -05:00
Benjamin Walsh
fc1c0c3345 libc/minimal: use per-thread errno implementation
Provides proper access to the per-thread errno variable, by using the
_get_errno() call.

Users can now do:

  errno = EINVAL;
  printk("errno: %d\n", errno);

Change-Id: I0ef365199656d002623b39b7f45f14f561501375
Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-02-05 20:24:58 -05:00
Allan Stephens
bf223a8838 stdio: Fix bug in fputs()
The previous implementation mistakenly kept outputting the first
character of the string endlessly.

Change-Id: I299c627a52158218be8e88c952935edb87a636fe
Suggested-by: Tom Yeon <tom.yeon@windriver.com>
Signed-off-by: Allan Stephens <allan.stephens@windriver.com>
2016-02-05 20:24:44 -05:00
Dan Kalowsky
486c382bfe checkpatch: warning - block_comment_style
Change-Id: Ib2c6b713dd74e8b24a9a4a636f8923ae21c7c25e
Signed-off-by: Dan Kalowsky <daniel.kalowsky@intel.com>
2016-02-05 20:24:35 -05:00
Dan Kalowsky
070a6e3ec9 checkpatch: error - trailing_whitespace
Change-Id: Id096b9eabcd8468e9343ceae5444c7726a1c539e
Signed-off-by: Dan Kalowsky <daniel.kalowsky@intel.com>
2016-02-05 20:24:35 -05:00
Dan Kalowsky
72f0a1314e checkpatch: warning - unnecssary_else
Change-Id: I6cc45cfcf09448b8fc988dc56219ea7560636af4
Signed-off-by: Dan Kalowsky <daniel.kalowsky@intel.com>
2016-02-05 20:24:35 -05:00
Dan Kalowsky
e45956c9a0 checkpatch: warning - leading_space
Change-Id: I34a54230bc4a63c8a4391d03ff530835910c3705
Signed-off-by: Dan Kalowsky <daniel.kalowsky@intel.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2016-02-05 20:24:33 -05:00
Dan Kalowsky
a92e48890c checkpatch: error - global_initialisers
Change-Id: Ib3f69be9f9273e57952a3ebceebc82d84c607c64
Signed-off-by: Dan Kalowsky <daniel.kalowsky@intel.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2016-02-05 20:24:33 -05:00
Dan Kalowsky
da67b29569 checkpatch: warning - block_comment_style
Change-Id: I6da43e41f9c6efee577b70513ec368ae3cce0144
Signed-off-by: Dan Kalowsky <daniel.kalowsky@intel.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2016-02-05 20:24:33 -05:00
Dan Kalowsky
e4bb957a00 checkpatch: error - pointer_location
Change-Id: I9ce224cb75ba21caecffd6747833257f748b5ecf
Signed-off-by: Dan Kalowsky <daniel.kalowsky@intel.com>
2016-02-05 20:24:31 -05:00