Commit Graph

242 Commits

Author SHA1 Message Date
Michael Hope 5d55730cf6 libc: minimal: add size optimized string functions
The current implementations of memcpy and memset are optimized for
performance and use a word based loop before the byte based loop.

Add a config option that skips the word based loop. This saves 120
bytes on the Cortex-M0+ which is worthwhile on small apps like a
bootloader.

Enable by default if SIZE_OPTIMIZATIONS is set.

Signed-off-by: Michael Hope <mlhx@google.com>
2021-07-06 07:40:38 -04:00
Maksim Masalski 8535a01d92 libc: types: add a comment to the "wrong definition" code
Add an explanation comment, so no one in the future
will try to change that part of the code.
Add parasoft tags to suppress a violation in static analysis tool

Signed-off-by: Maksim Masalski <maksim.masalski@intel.com>
2021-06-24 09:29:39 +02:00
Maksim Masalski a61edd480d lib: add default labels and comments to switch statements
According to the Zephyr Coding Guideline all switch statements
shall be well-formed.
Added a default labels to switch-clauses without them.
Added comments to the empty default cases.

Found as a coding guideline violation (MISRA R16.1) by static
coding scanning tool.

Signed-off-by: Maksim Masalski <maksim.masalski@intel.com>
2021-06-04 16:22:23 -05:00
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
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
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
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
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 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
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
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
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
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 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
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
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
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
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
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
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
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
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
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
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
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 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
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
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
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
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
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
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
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
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
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
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