1171568Sscottl/*
2211095Sdes * Australian Public Licence B (OZPLB)
3171568Sscottl *
4171568Sscottl * Version 1-0
5171568Sscottl *
6171568Sscottl * Copyright (c) 2004 National ICT Australia
7171568Sscottl *
8171568Sscottl * All rights reserved.
9171568Sscottl *
10171568Sscottl * Developed by: Embedded, Real-time and Operating Systems Program (ERTOS)
11171568Sscottl *               National ICT Australia
12171568Sscottl *               http://www.ertos.nicta.com.au
13171568Sscottl *
14171568Sscottl * Permission is granted by National ICT Australia, free of charge, to
15171568Sscottl * any person obtaining a copy of this software and any associated
16171568Sscottl * documentation files (the "Software") to deal with the Software without
17171568Sscottl * restriction, including (without limitation) the rights to use, copy,
18171568Sscottl * modify, adapt, merge, publish, distribute, communicate to the public,
19171568Sscottl * sublicense, and/or sell, lend or rent out copies of the Software, and
20171568Sscottl * to permit persons to whom the Software is furnished to do so, subject
21171568Sscottl * to the following conditions:
22171568Sscottl *
23171568Sscottl *     * Redistributions of source code must retain the above copyright
24171568Sscottl *       notice, this list of conditions and the following disclaimers.
25171568Sscottl *
26171568Sscottl *     * Redistributions in binary form must reproduce the above
27171568Sscottl *       copyright notice, this list of conditions and the following
28171568Sscottl *       disclaimers in the documentation and/or other materials provided
29211095Sdes *       with the distribution.
30171568Sscottl *
31171568Sscottl *     * Neither the name of National ICT Australia, nor the names of its
32171568Sscottl *       contributors, may be used to endorse or promote products derived
33171568Sscottl *       from this Software without specific prior written permission.
34171568Sscottl *
35171568Sscottl * EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
36171568Sscottl * PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS", AND
37171568Sscottl * NATIONAL ICT AUSTRALIA AND ITS CONTRIBUTORS MAKE NO REPRESENTATIONS,
38171568Sscottl * WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
39171568Sscottl * BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS
40211095Sdes * REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE,
41171568Sscottl * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT,
42171568Sscottl * THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF
43171568Sscottl * ERRORS, WHETHER OR NOT DISCOVERABLE.
44171568Sscottl *
45171568Sscottl * TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
46171568Sscottl * NATIONAL ICT AUSTRALIA OR ITS CONTRIBUTORS BE LIABLE ON ANY LEGAL
47171568Sscottl * THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION OF CONTRACT,
48171568Sscottl * NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER
49171568Sscottl * LIABILITY, INCLUDING (WITHOUT LIMITATION) LOSS OF PRODUCTION OR
50171568Sscottl * OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS
51171568Sscottl * OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR
52171568Sscottl * OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT,
53171568Sscottl * CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN
54171568Sscottl * CONNECTION WITH THIS LICENCE, THE SOFTWARE OR THE USE OF OR OTHER
55171568Sscottl * DEALINGS WITH THE SOFTWARE, EVEN IF NATIONAL ICT AUSTRALIA OR ITS
56171568Sscottl * CONTRIBUTORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS,
57171568Sscottl * DAMAGES OR OTHER LIABILITY.
58171568Sscottl *
59171568Sscottl * If applicable legislation implies representations, warranties, or
60171568Sscottl * conditions, or imposes obligations or liability on National ICT
61171568Sscottl * Australia or one of its contributors in respect of the Software that
62171568Sscottl * cannot be wholly or partly excluded, restricted or modified, the
63171568Sscottl * liability of National ICT Australia or the contributor is limited, to
64171568Sscottl * the full extent permitted by the applicable legislation, at its
65171568Sscottl * option, to:
66171568Sscottl * a.  in the case of goods, any one or more of the following:
67171568Sscottl * i.  the replacement of the goods or the supply of equivalent goods;
68171568Sscottl * ii.  the repair of the goods;
69171568Sscottl * iii. the payment of the cost of replacing the goods or of acquiring
70171568Sscottl *  equivalent goods;
71171568Sscottl * iv.  the payment of the cost of having the goods repaired; or
72171568Sscottl * b.  in the case of services:
73171568Sscottl * i.  the supplying of the services again; or
74171568Sscottl * ii.  the payment of the cost of having the services supplied again.
75171568Sscottl *
76171568Sscottl * The construction, validity and performance of this licence is governed
77171568Sscottl * by the laws in force in New South Wales, Australia.
78171568Sscottl */
79171568Sscottl
80171568Sscottl/*
81171568Sscottl * Copyright (c) 2011, ETH Zurich.
82171568Sscottl * All rights reserved.
83171568Sscottl *
84171568Sscottl * This file is distributed under the terms in the attached LICENSE file.
85171568Sscottl * If you do not find this file, copies can be found by writing to:
86171568Sscottl * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
87171568Sscottl */
88171568Sscottl
89171568Sscottl#define _USE_XOPEN
90171568Sscottl#include <time.h>
91171568Sscottl#include <assert.h>
92171568Sscottl#include <barrelfish/barrelfish.h>
93171568Sscottl#include <barrelfish/sys_debug.h>
94171568Sscottl
95171568Sscottl// XXX: Fake Epoch offset, until we have an RTC driver
96171568Sscottl// This is roughly Mon Apr 25 13:50 CEST 2011
97171568Sscottl#define TOD_OFFSET      1303732456ULL
98171568Sscottl
99171568Sscottltime_t
100171568Sscottltime(time_t *timer)
101171568Sscottl{
102171568Sscottl    // XXX: What about TSC overflow?
103171568Sscottl    uint64_t now = rdtsc();
104171568Sscottl
105171568Sscottl#if defined(__arm__)
106171568Sscottl    uint32_t tscrate;
107171568Sscottl    errval_t err = sys_debug_hardware_timer_hertz_read(&tscrate);
108171568Sscottl    assert(err_is_ok(err));
109171568Sscottl
110171568Sscottl    assert(tscrate > 0);
111171568Sscottl    uint64_t tod = TOD_OFFSET + (now / tscrate);
112171568Sscottl#else
113171568Sscottl    uint64_t tscperms;
114171568Sscottl    errval_t err = sys_debug_get_tsc_per_ms(&tscperms);
115171568Sscottl    assert(err_is_ok(err));
116171568Sscottl
117171568Sscottl    assert(tscperms > 0);
118171568Sscottl    uint64_t tod = TOD_OFFSET + (now / (tscperms * 1000));
119171568Sscottl#endif
120171568Sscottl
121171568Sscottl    time_t val = tod;
122171568Sscottl    if (timer)
123171568Sscottl        *timer = val;
124171568Sscottl    return val;
125171568Sscottl}
126171568Sscottl