flat assembler
Message board for the users of flat assembler.

Index > Linux > How to make timer?

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 24 Mar 2011, 14:58
Classic question to many Linux forums, that usually is answered with "There is a Google!" or "We don't want to make your homework for you." Very Happy
So - don't answer in this manner!

After some search, I figured out that there are several "kernel functions" - such as "init_timer", "add_timer" and so on, that allows creating of timer and running callback after some predefined time.
Unfortunately, I didn't figured out how I can use these functions from assembly program.
So, please help if someone knows how I can use timers in assembly program.

P.S. Linking to static libraries is not an option. Importing from shared libraries is OK. Using only syscals is the best, but I am afraid it is not possible in this case.
Post 24 Mar 2011, 14:58
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20689
Location: In your JS exploiting you and your system
revolution 24 Mar 2011, 15:06
Perhaps you should also state what resolution in timing you need. Microsecond delays are very different, and use different techniques, compared to hour delays.
Post 24 Mar 2011, 15:06
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 24 Mar 2011, 15:48
From 50ms to several seconds.
Post 24 Mar 2011, 15:48
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 24 Mar 2011, 17:53
What about using the following syscalls:
Code:
sys_setitimer                 = 068h
sys_getitimer                 = 069h
    

I'll try to produce a working snippet later at home
Post 24 Mar 2011, 17:53
View user's profile Send private message Yahoo Messenger Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 24 Mar 2011, 18:46
pelaillo wrote:
What about using the following syscalls:
Code:
sys_setitimer                 = 068h
sys_getitimer                 = 069h
    

I'll try to produce a working snippet later at home


The thing that stopped me to use setitimer functions is that there is only one timer available, so I have to create special library in order to allow several timers to be used in the program.
But when I think now - it looks not so bad idea...
Post 24 Mar 2011, 18:46
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1215
Location: NC, USA
Tyler 24 Mar 2011, 19:26
Has that library not already been created? It seems like a necessity, surly someone has done it already.
Post 24 Mar 2011, 19:26
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 24 Mar 2011, 20:58
Tyler wrote:
Has that library not already been created? It seems like a necessity, surly someone has done it already.


There is a C implementation. I don't know about any FASM implementation.
So, probably it is good idea to make one for FreshLib. It will be portable of course.
Post 24 Mar 2011, 20:58
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7103
Location: Slovakia
vid 24 Mar 2011, 22:12
Same issue as with heap: Linux kernel only offers smallest set needed to implement the library, not the library itself. It is not expected someone would use syscalls instead of "proper" using of glibc.

BTW, why don't you use glibc? It might save you few headaches.
Post 24 Mar 2011, 22:12
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 24 Mar 2011, 22:45
vid wrote:
BTW, why don't you use glibc? It might save you few headaches.


I would (maybe) but there are thousands of C libraries and what is the proper one? Evil or Very Mad Are there some timer functions in glibc? And what is the name of the shared library that you call "glibc"? I am using several functions from libc (actually named libc.so.6) but there is no "glibc.so.* in my installation. But there is libgcc_s.so.1
Linux is a mess of files, links to files, links to links to files and some objects that look and behave like files, but actually are not.... and all these files are text files! Laughing
Post 24 Mar 2011, 22:45
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1215
Location: NC, USA
Tyler 24 Mar 2011, 22:52
libgcc is internal gcc stuff, that it links into your C(or other) code.

But, as for finding which thingy to link to: USE MAN!!! Smile

Code:
$man libc
LIBC(7)                    Linux Programmer's Manual                   LIBC(7)

NAME
       libc - Overview of standard C libraries on Linux

DESCRIPTION
       The  term  "libc"  is  commonly used as a shorthand for the "standard C
       library", a library of standard functions that can be  used  by  all  C
       programs  (and  sometimes  by programs in other languages).  Because of
       some history (see below), use of the term "libc" to refer to the  stan
       dard C library is somewhat ambiguous on Linux.

   glibc
       By  far  the  most  widely used C library on Linux is the GNU C Library
       (http://www.gnu.org/software/libc/), often referred to as glibc.   This
       is  the  C  library  that is nowadays used in all major Linux distribu
       tions.  It is also the C library whose details are  documented  in  the
       relevant  pages of the man-pages project (primarily in Section 3 of the
       manual).  Documentation of glibc is also available in the glibc manual,
       available  via the command info libc.  Release 1.0 of glibc was made in
       September 1992.  (There were earlier 0.x  releases.)   The  next  major
       release of glibc was 2.0, at the beginning of 1997.

       ---=== The  pathname  /lib/libc.so.6 (or something similar) is normally a sym
       bolic link that points to the location of the glibc library ===--- ,  and  exe
       cuting  this  pathname  will cause glibc to display various information
       about the version installed on your system.
    
Post 24 Mar 2011, 22:52
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 24 Mar 2011, 23:11
libc (or whatever it is called today) does not contains decent timers support.
Post 24 Mar 2011, 23:11
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1215
Location: NC, USA
Tyler 24 Mar 2011, 23:15
Moreover, it contains no timer support. You were the one asking about libc, not me. Smile
Post 24 Mar 2011, 23:15
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7103
Location: Slovakia
vid 24 Mar 2011, 23:24
It should at minimal offer some kind of wrapper over kernel function, doesn't it?
Post 24 Mar 2011, 23:24
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 24 Mar 2011, 23:33
There are both getitimer and setitimer functions, they are defined as "weak" symbols. (What that means?)
But why I have to use wrappers if I can use the originals?
Post 24 Mar 2011, 23:33
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1215
Location: NC, USA
Tyler 25 Mar 2011, 00:58
vid: POSIX does. libc does not.

John: Try this: http://www.gnu.org/s/libc/manual/html_node/Setting-an-Alarm.html. It's POSIX, which is almost as portable as libc. As for an explanation of weak symbols, try http://en.wikipedia.org/wiki/Weak_symbol.
Post 25 Mar 2011, 00:58
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7103
Location: Slovakia
vid 25 Mar 2011, 08:45
Quote:
vid: POSIX does. libc does not.

Right, thanks. But then I am wondering, in what library is code of these wrappers physically present?
Post 25 Mar 2011, 08:45
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 26 Mar 2011, 13:10
You guys, misguided me! I discovered, that the kernel actually supports multiple timers. The functions are:
Code:
sys_timer_create         =  $103
sys_timer_settime        =  $104
sys_timer_gettime        =  $105
sys_timer_getoverrun     =  $106
sys_timer_delete         =  $107
    

Too bad - I just finished library, using setitimer functions. Now I have to rewrite it from scratch.

Regards.
Post 26 Mar 2011, 13:10
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1215
Location: NC, USA
Tyler 27 Mar 2011, 17:10
vid wrote:
Quote:
vid: POSIX does. libc does not.

Right, thanks. But then I am wondering, in what library is code of these wrappers physically present?
Probably glibc... Razz

John: It was a conspiracy all along. We were just out to waste your time. Smile
Post 27 Mar 2011, 17:10
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 28 Mar 2011, 06:53
Later news from the big timer battle. Razz

POSIX timers appears to support only signal events in the kernel (and callback call events in the glibc wrapper - I really hate how libc and C are enforced in Linux programming)

Anyway - signal handlers in Linux are very, very restrictive - they can only change memory and call limited number of functions.
Because this, I tried to make the timers to use threads - when the timer expires - the user callback function is started in new thread and after processing ends.
This approach appears to work - you can see the sources in the file linux/timer.asm
The good thing of the whole story is that now we have almost working Linux threads in FreshLib.
Post 28 Mar 2011, 06:53
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20689
Location: In your JS exploiting you and your system
revolution 28 Mar 2011, 07:11
Ouch. Using threads for timer expiry sounds very expensive in both memory usage and OS overhead. Isn't there another way?
Post 28 Mar 2011, 07:11
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.