Skip to content

Commit 6e2ef5e

Browse files
author
Martin Schwidefsky
committed
s390/time: add support for the TOD clock epoch extension
The TOD epoch extension adds 8 epoch bits to the TOD clock to provide a continuous clock after 2042/09/17. The store-clock-extended (STCKE) instruction will store the epoch index in the first byte of the 16 bytes stored by the instruction. The read_boot_clock64 and the read_presistent_clock64 functions need to take the additional bits into account to give the correct result after 2042/09/17. The clock-comparator register will stay 64 bit wide. The comparison of the clock-comparator with the TOD clock is limited to bytes 1 to 8 of the extended TOD format. To deal with the overflow problem due to an epoch change the clock-comparator sign control in CR0 can be used to switch the comparison of the 64-bit TOD clock with the clock-comparator to a signed comparison. The decision between the signed vs. unsigned clock-comparator comparisons is done at boot time. Only if the TOD clock is in the second half of a 142 year epoch the signed comparison is used. This solves the epoch overflow issue as long as the machine is booted at least once in an epoch. Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
1 parent 45be0a0 commit 6e2ef5e

12 files changed

Lines changed: 130 additions & 62 deletions

File tree

‎arch/s390/include/asm/lowcore.h‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,46 +95,46 @@ struct lowcore {
9595
__u64 int_clock; /* 0x0310 */
9696
__u64 mcck_clock; /* 0x0318 */
9797
__u64 clock_comparator; /* 0x0320 */
98+
__u64 boot_clock[2]; /* 0x0328 */
9899

99100
/* Current process. */
100-
__u64 current_task; /* 0x0328 */
101-
__u8 pad_0x318[0x320-0x318]; /* 0x0330 */
102-
__u64 kernel_stack; /* 0x0338 */
101+
__u64 current_task; /* 0x0338 */
102+
__u64 kernel_stack; /* 0x0340 */
103103

104104
/* Interrupt, panic and restart stack. */
105-
__u64 async_stack; /* 0x0340 */
106-
__u64 panic_stack; /* 0x0348 */
107-
__u64 restart_stack; /* 0x0350 */
105+
__u64 async_stack; /* 0x0348 */
106+
__u64 panic_stack; /* 0x0350 */
107+
__u64 restart_stack; /* 0x0358 */
108108

109109
/* Restart function and parameter. */
110-
__u64 restart_fn; /* 0x0358 */
111-
__u64 restart_data; /* 0x0360 */
112-
__u64 restart_source; /* 0x0368 */
110+
__u64 restart_fn; /* 0x0360 */
111+
__u64 restart_data; /* 0x0368 */
112+
__u64 restart_source; /* 0x0370 */
113113

114114
/* Address space pointer. */
115-
__u64 kernel_asce; /* 0x0370 */
116-
__u64 user_asce; /* 0x0378 */
115+
__u64 kernel_asce; /* 0x0378 */
116+
__u64 user_asce; /* 0x0380 */
117117

118118
/*
119119
* The lpp and current_pid fields form a
120120
* 64-bit value that is set as program
121121
* parameter with the LPP instruction.
122122
*/
123-
__u32 lpp; /* 0x0380 */
124-
__u32 current_pid; /* 0x0384 */
123+
__u32 lpp; /* 0x0388 */
124+
__u32 current_pid; /* 0x038c */
125125

126126
/* SMP info area */
127-
__u32 cpu_nr; /* 0x0388 */
128-
__u32 softirq_pending; /* 0x038c */
129-
__u64 percpu_offset; /* 0x0390 */
130-
__u64 vdso_per_cpu_data; /* 0x0398 */
131-
__u64 machine_flags; /* 0x03a0 */
132-
__u32 preempt_count; /* 0x03a8 */
133-
__u8 pad_0x03ac[0x03b0-0x03ac]; /* 0x03ac */
134-
__u64 gmap; /* 0x03b0 */
135-
__u32 spinlock_lockval; /* 0x03b8 */
136-
__u32 fpu_flags; /* 0x03bc */
137-
__u8 pad_0x03c0[0x0400-0x03c0]; /* 0x03c0 */
127+
__u32 cpu_nr; /* 0x0390 */
128+
__u32 softirq_pending; /* 0x0394 */
129+
__u64 percpu_offset; /* 0x0398 */
130+
__u64 vdso_per_cpu_data; /* 0x03a0 */
131+
__u64 machine_flags; /* 0x03a8 */
132+
__u32 preempt_count; /* 0x03b0 */
133+
__u8 pad_0x03b4[0x03b8-0x03b4]; /* 0x03b4 */
134+
__u64 gmap; /* 0x03b8 */
135+
__u32 spinlock_lockval; /* 0x03c0 */
136+
__u32 fpu_flags; /* 0x03c4 */
137+
__u8 pad_0x03c8[0x0400-0x03c8]; /* 0x03c8 */
138138

139139
/* Per cpu primary space access list */
140140
__u32 paste[16]; /* 0x0400 */

‎arch/s390/include/asm/setup.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define MACHINE_FLAG_TLB_GUEST _BITUL(14)
3333
#define MACHINE_FLAG_NX _BITUL(15)
3434
#define MACHINE_FLAG_GS _BITUL(16)
35+
#define MACHINE_FLAG_SCC _BITUL(17)
3536

3637
#define LPP_MAGIC _BITUL(31)
3738
#define LPP_PFAULT_PID_MASK _AC(0xffffffff, UL)
@@ -72,6 +73,7 @@ extern void detect_memory_memblock(void);
7273
#define MACHINE_HAS_TLB_GUEST (S390_lowcore.machine_flags & MACHINE_FLAG_TLB_GUEST)
7374
#define MACHINE_HAS_NX (S390_lowcore.machine_flags & MACHINE_FLAG_NX)
7475
#define MACHINE_HAS_GS (S390_lowcore.machine_flags & MACHINE_FLAG_GS)
76+
#define MACHINE_HAS_SCC (S390_lowcore.machine_flags & MACHINE_FLAG_SCC)
7577

7678
/*
7779
* Console mode. Override with conmode=

‎arch/s390/include/asm/timex.h‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
/* The value of the TOD clock for 1.1.1970. */
1616
#define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
1717

18+
extern u64 clock_comparator_max;
19+
1820
/* Inline functions for clock register access. */
1921
static inline int set_tod_clock(__u64 time)
2022
{
@@ -126,7 +128,7 @@ static inline unsigned long long local_tick_disable(void)
126128
unsigned long long old;
127129

128130
old = S390_lowcore.clock_comparator;
129-
S390_lowcore.clock_comparator = -1ULL;
131+
S390_lowcore.clock_comparator = clock_comparator_max;
130132
set_clock_comparator(S390_lowcore.clock_comparator);
131133
return old;
132134
}
@@ -178,20 +180,20 @@ int get_phys_clock(unsigned long long *clock);
178180
void init_cpu_timer(void);
179181
unsigned long long monotonic_clock(void);
180182

181-
extern u64 sched_clock_base_cc;
183+
extern unsigned char tod_clock_base[16] __aligned(8);
182184

183185
/**
184186
* get_clock_monotonic - returns current time in clock rate units
185187
*
186188
* The caller must ensure that preemption is disabled.
187-
* The clock and sched_clock_base get changed via stop_machine.
189+
* The clock and tod_clock_base get changed via stop_machine.
188190
* Therefore preemption must be disabled when calling this
189191
* function, otherwise the returned value is not guaranteed to
190192
* be monotonic.
191193
*/
192194
static inline unsigned long long get_tod_clock_monotonic(void)
193195
{
194-
return get_tod_clock() - sched_clock_base_cc;
196+
return get_tod_clock() - *(unsigned long long *) &tod_clock_base[1];
195197
}
196198

197199
/**
@@ -218,4 +220,32 @@ static inline unsigned long long tod_to_ns(unsigned long long todval)
218220
return ((todval >> 9) * 125) + (((todval & 0x1ff) * 125) >> 9);
219221
}
220222

223+
/**
224+
* tod_after - compare two 64 bit TOD values
225+
* @a: first 64 bit TOD timestamp
226+
* @b: second 64 bit TOD timestamp
227+
*
228+
* Returns: true if a is later than b
229+
*/
230+
static inline int tod_after(unsigned long long a, unsigned long long b)
231+
{
232+
if (MACHINE_HAS_SCC)
233+
return (long long) a > (long long) b;
234+
return a > b;
235+
}
236+
237+
/**
238+
* tod_after_eq - compare two 64 bit TOD values
239+
* @a: first 64 bit TOD timestamp
240+
* @b: second 64 bit TOD timestamp
241+
*
242+
* Returns: true if a is later than b
243+
*/
244+
static inline int tod_after_eq(unsigned long long a, unsigned long long b)
245+
{
246+
if (MACHINE_HAS_SCC)
247+
return (long long) a >= (long long) b;
248+
return a >= b;
249+
}
250+
221251
#endif

‎arch/s390/kernel/asm-offsets.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ int main(void)
158158
OFFSET(__LC_LAST_UPDATE_CLOCK, lowcore, last_update_clock);
159159
OFFSET(__LC_INT_CLOCK, lowcore, int_clock);
160160
OFFSET(__LC_MCCK_CLOCK, lowcore, mcck_clock);
161+
OFFSET(__LC_BOOT_CLOCK, lowcore, boot_clock);
161162
OFFSET(__LC_CURRENT, lowcore, current_task);
162163
OFFSET(__LC_KERNEL_STACK, lowcore, kernel_stack);
163164
OFFSET(__LC_ASYNC_STACK, lowcore, async_stack);

‎arch/s390/kernel/debug.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,8 @@ static inline void
866866
debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
867867
int exception)
868868
{
869-
active->id.stck = get_tod_clock_fast() - sched_clock_base_cc;
869+
active->id.stck = get_tod_clock_fast() -
870+
*(unsigned long long *) &tod_clock_base[1];
870871
active->id.fields.cpuid = smp_processor_id();
871872
active->caller = __builtin_return_address(0);
872873
active->id.fields.exception = exception;
@@ -1455,15 +1456,15 @@ int
14551456
debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
14561457
int area, debug_entry_t * entry, char *out_buf)
14571458
{
1458-
unsigned long sec, usec;
1459+
unsigned long base, sec, usec;
14591460
char *except_str;
14601461
unsigned long caller;
14611462
int rc = 0;
14621463
unsigned int level;
14631464

14641465
level = entry->id.fields.level;
1465-
sec = (entry->id.stck >> 12) + (sched_clock_base_cc >> 12);
1466-
sec = sec - (TOD_UNIX_EPOCH >> 12);
1466+
base = (*(unsigned long *) &tod_clock_base[0]) >> 4;
1467+
sec = (entry->id.stck >> 12) + base - (TOD_UNIX_EPOCH >> 12);
14671468
usec = do_div(sec, USEC_PER_SEC);
14681469

14691470
if (entry->id.fields.exception)

‎arch/s390/kernel/early.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ static void __init reset_tod_clock(void)
5353
if (set_tod_clock(TOD_UNIX_EPOCH) != 0 || store_tod_clock(&time) != 0)
5454
disabled_wait(0);
5555

56-
sched_clock_base_cc = TOD_UNIX_EPOCH;
57-
S390_lowcore.last_update_clock = sched_clock_base_cc;
56+
memset(tod_clock_base, 0, 16);
57+
*(__u64 *) &tod_clock_base[1] = TOD_UNIX_EPOCH;
58+
S390_lowcore.last_update_clock = TOD_UNIX_EPOCH;
5859
}
5960

6061
#ifdef CONFIG_SHARED_KERNEL
@@ -165,8 +166,8 @@ static noinline __init void create_kernel_nss(void)
165166
}
166167

167168
/* re-initialize cputime accounting. */
168-
sched_clock_base_cc = get_tod_clock();
169-
S390_lowcore.last_update_clock = sched_clock_base_cc;
169+
get_tod_clock_ext(tod_clock_base);
170+
S390_lowcore.last_update_clock = *(__u64 *) &tod_clock_base[1];
170171
S390_lowcore.last_update_timer = 0x7fffffffffffffffULL;
171172
S390_lowcore.user_timer = 0;
172173
S390_lowcore.system_timer = 0;
@@ -387,6 +388,12 @@ static __init void detect_machine_facilities(void)
387388
}
388389
if (test_facility(133))
389390
S390_lowcore.machine_flags |= MACHINE_FLAG_GS;
391+
if (test_facility(139) && (tod_clock_base[1] & 0x80)) {
392+
/* Enabled signed clock comparator comparisons */
393+
S390_lowcore.machine_flags |= MACHINE_FLAG_SCC;
394+
clock_comparator_max = -1ULL >> 1;
395+
__ctl_set_bit(0, 53);
396+
}
390397
}
391398

392399
static inline void save_vector_registers(void)

‎arch/s390/kernel/head.S‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ ENTRY(startup_kdump)
302302
xc 0xe00(256),0xe00
303303
xc 0xf00(256),0xf00
304304
lctlg %c0,%c15,0x200(%r0) # initialize control registers
305-
stck __LC_LAST_UPDATE_CLOCK
305+
stcke __LC_BOOT_CLOCK
306+
mvc __LC_LAST_UPDATE_CLOCK(8),__LC_BOOT_CLOCK+1
306307
spt 6f-.LPG0(%r13)
307308
mvc __LC_LAST_UPDATE_TIMER(8),6f-.LPG0(%r13)
308309
l %r15,.Lstack-.LPG0(%r13)

‎arch/s390/kernel/head64.S‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ ENTRY(startup_continue)
2121
xc __LC_LPP+1(7,0),__LC_LPP+1 # clear lpp and current_pid
2222
mvi __LC_LPP,0x80 # and set LPP_MAGIC
2323
.insn s,0xb2800000,__LC_LPP # load program parameter
24-
0: larl %r1,sched_clock_base_cc
25-
mvc 0(8,%r1),__LC_LAST_UPDATE_CLOCK
24+
0: larl %r1,tod_clock_base
25+
mvc 0(16,%r1),__LC_BOOT_CLOCK
2626
larl %r13,.LPG1 # get base
2727
lctlg %c0,%c15,.Lctl-.LPG1(%r13) # load control registers
2828
lg %r12,.Lparmaddr-.LPG1(%r13) # pointer to parameter area

‎arch/s390/kernel/irq.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ void do_IRQ(struct pt_regs *regs, int irq)
105105

106106
old_regs = set_irq_regs(regs);
107107
irq_enter();
108-
if (S390_lowcore.int_clock >= S390_lowcore.clock_comparator)
108+
if (tod_after_eq(S390_lowcore.int_clock,
109+
S390_lowcore.clock_comparator))
109110
/* Serve timer interrupts first. */
110111
clock_comparator_work();
111112
generic_handle_irq(irq);

‎arch/s390/kernel/setup.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ static void __init setup_lowcore(void)
323323
lc->io_new_psw.mask = PSW_KERNEL_BITS |
324324
PSW_MASK_DAT | PSW_MASK_MCHECK;
325325
lc->io_new_psw.addr = (unsigned long) io_int_handler;
326-
lc->clock_comparator = -1ULL;
326+
lc->clock_comparator = clock_comparator_max;
327327
lc->kernel_stack = ((unsigned long) &init_thread_union)
328328
+ THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
329329
lc->async_stack = (unsigned long)

0 commit comments

Comments
 (0)