Arikania
Posts: 15
Joined: Tue Sep 11, 2018 6:44 am

sys_time not implemented

Tue May 21, 2019 2:58 am

I work with Linux Jessie on an ARMv7-A processor, using the GNU assembler.

I found that sys_time returns ENOSYS in both these examples, indicating that the system call is not implemented:

Code: Select all

movs	r0,#0		@ pointer to the variable that the time is to be stored in
movs	r7,#13		@ function #
svc	#0

Code: Select all

sub	sp,#8
mov	r0,sp
movs	r7,#13
svc	#0
add	sp,#8

According to man 2 time, both these codes should be correct.

What is going wrong, and which system call should I use instead?


Kindly,


Arikania

LdB
Posts: 1143
Joined: Wed Dec 07, 2016 2:29 pm

Re: sys_time not implemented

Tue May 21, 2019 7:17 am

Probably to do with the fact there is no RTC on Pi.

The only way it can get date and time is if connected to internet.

jahboater
Posts: 4453
Joined: Wed Feb 04, 2015 6:38 pm

Re: sys_time not implemented

Tue May 21, 2019 7:57 am

For some reason SYS_time is not implemented for ARM.

The library function time() instead uses the POSIX function.
The code is similar to this:

Code: Select all

#include <time.h>

  struct timespec now;
  clock_gettime( CLOCK_REALTIME, &now );
  return now.tv_sec;

Arikania
Posts: 15
Joined: Tue Sep 11, 2018 6:44 am

Re: sys_time not implemented

Tue May 21, 2019 8:53 am

Thank you for your quick replies :D

The RaspBerry is connected to internet, and in touch with an NTP server. It hosts a website that receives UTC from my server, and displays it correctly.

I am now indeed busy trying the solution with sys_clock_gettime, but at first glance, it appears to always return 0:

Code: Select all

.type gettime, %function
.balign 4


0:	.word 265		@ function number

.thumb_func
gettime:
  @ at return:
  # r0	seconds since epoch
  # r1	nanosecs since r0


	# init
	push	{ r7 }

	adr	r0,0b		@ retrieve the function number
	ldr	r7,[r0]

	sub	sp,#8		@ create space for the result

	# retrieve the time
	movs	r0,#0		@ clock id, as found in the header files
	mov	r1,sp		@ see note below
	svc	#0

	pop	{ r0-r1, r7 }	@ retrieve the time from the structure
	bx	lr		@ exit

note:
Upon using random values for r1 (including -1 !!), I learned that the system call does not utilize this register :?: :!:

Also, even though date shows the correct date and time, the time command shows me this:

Code: Select all

real	0m0.000s
user	0m0.000s
sys	0m0.000s

Am I using the wrong function still?? I'll keep you apprised!


A.

jahboater
Posts: 4453
Joined: Wed Feb 04, 2015 6:38 pm

Re: sys_time not implemented

Tue May 21, 2019 9:04 am

The time command is nothing to do with the time() library function.

The time command is (usually) a shell builtin that prints execution times for the following command.

The time() library function returns the number of seconds since the epoch as a "time_t".

time_t might be 64-bits of course.

Edit: it is not, on Raspbian time_t is 32-bits!
Hopefully Raspbian will be 64-bit before 2038 :)

The function number SYS_clock_gettime is 263 by the way, not 265
(at least on the latest Raspbian on my Pi3B+)

Could you just use "movw r7,#263" ?

Arikania
Posts: 15
Joined: Tue Sep 11, 2018 6:44 am

Re: sys_time not implemented

Tue May 21, 2019 9:25 am

I am responding right away since you are online at this very moment.

I did a little more checking, and for some reason, the listing that I showed triggers EFAULT, indicating an invalid address.

But that would merely mean that the copy of r7 would be destroyed. I tried reserving 128 bytes of stack space now, but still get this error.

It would be great if we could look up the source codes of those shell commands somewhere or, rather, their assembly equivalents.

I am eagerly awaiting a headless 64-bit Raspbian with ditto fs support for my other Pi indeed, which has an ARMv8, but of which Jessie ARMv7-A cannot use all prowess, such as its fpu''s.


With regards, A.

jahboater
Posts: 4453
Joined: Wed Feb 04, 2015 6:38 pm

Re: sys_time not implemented

Tue May 21, 2019 9:33 am

Arikania wrote:
Tue May 21, 2019 9:25 am
I am eagerly awaiting a headless 64-bit Raspbian with ditto fs support for my other Pi indeed, which has an ARMv8, but of which Jessie ARMv7-A cannot use all prowess, such as its fpu''s.
You can use the FPU (hardware floating-point) on all Pi models.

For the older Pi's, that is VFP, but for the ARMv7 and later, it is NEON.

The only difference in 64-bit mode is that the NEON register layout is more sensible.
(and obviously the assembly language syntax is nicer).

jahboater
Posts: 4453
Joined: Wed Feb 04, 2015 6:38 pm

Re: sys_time not implemented

Tue May 21, 2019 9:37 am

Arikania wrote:
Tue May 21, 2019 9:25 am
It would be great if we could look up the source codes of those shell commands somewhere or, rather, their assembly equivalents.
You can get the glibc source from here
https://ftp.gnu.org/gnu/glibc/
Most of it is written in C of course.

Why not start with something really simple, say write a little "hello world" program (two system calls), and then try clock_gettime.

jahboater
Posts: 4453
Joined: Wed Feb 04, 2015 6:38 pm

Re: sys_time not implemented

Tue May 21, 2019 10:35 am

Incidentally, I get the actual system call numbers with a little C program:

Code: Select all

#include <stdio.h>
#include <sys/syscall.h>

int
main( void )
{
  printf( "gettime is %d\n", SYS_clock_gettime );
}
there may be a better way, but this is probably quicker than following it all back.

Arikania
Posts: 15
Joined: Tue Sep 11, 2018 6:44 am

[SOLVED] sys_time and sys_clock_gettime not implemented on Jessie

Thu May 23, 2019 5:07 am

The function in the previous post returned, for some reason, the function number of sys_timer_delete.

I did some more checking around, and it turns out that we need to use sys_gettimeofday (function #78), since sys_clock_gettime and sys_time are not implemented on Jessie for the ARMv7-A.


See man 2 gettimeofday for more info.


Thank you all for your patience and zeal. You guys are great!

The code that works for me now looks like this:

Code: Select all

.type gettime, %function
.balign 4

.thumb_func
gettime:
  @ at return:
  # r0		seconds since epoch
  # r1		microsecs since r0


	# init
	push	{ r7 }
	sub	sp,#8

	# retrieve the time
	mov	r0,sp
	movs	r1,#0
	movs	r7,#78
	svc	#0

	# exit
	pop	{ r0-r1, r7 }
	bx	lr

Love,

A.

Return to “Bare metal, Assembly language”