blob: 65400c32455b87e7f9372a8992f7d475df8999cc [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 * Portions Copyright (c) 2000 Ulrich Drepper
Wanlong Gao18190692012-09-18 17:14:01 +08004 *
plars865695b2001-08-27 22:15:12 +00005 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
Wanlong Gao18190692012-09-18 17:14:01 +08008 *
plars865695b2001-08-27 22:15:12 +00009 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Wanlong Gao18190692012-09-18 17:14:01 +080012 *
plars865695b2001-08-27 22:15:12 +000013 * Further, this software is distributed without any warranty that it is
14 * free of the rightful claim of any third person regarding infringement
15 * or the like. Any license provided herein, whether implied or
16 * otherwise, applies only to this software file. Patent licenses, if
17 * any, provided herein do not apply to combinations of this program with
18 * other software, or any other product whatsoever.
Wanlong Gao18190692012-09-18 17:14:01 +080019 *
plars865695b2001-08-27 22:15:12 +000020 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080021 * with this program; if not, write the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Wanlong Gao18190692012-09-18 17:14:01 +080023 *
plars865695b2001-08-27 22:15:12 +000024 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
25 * Mountain View, CA 94043, or:
Wanlong Gao18190692012-09-18 17:14:01 +080026 *
subrata_modak56207ce2009-03-23 13:35:39 +000027 * http://www.sgi.com$
Wanlong Gao18190692012-09-18 17:14:01 +080028 *
subrata_modak56207ce2009-03-23 13:35:39 +000029 * For further information regarding this notice, see:$
Wanlong Gao18190692012-09-18 17:14:01 +080030 *
plars865695b2001-08-27 22:15:12 +000031 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
Wanlong Gao18190692012-09-18 17:14:01 +080032 *
plars865695b2001-08-27 22:15:12 +000033 *
34 * Linux Test Project - Silicon Graphics, Inc.
plars865695b2001-08-27 22:15:12 +000035 * TEST IDENTIFIER : fork05
plars865695b2001-08-27 22:15:12 +000036 * EXECUTED BY : anyone
plars865695b2001-08-27 22:15:12 +000037 * TEST TITLE : Make sure LDT is propagated correctly
plars865695b2001-08-27 22:15:12 +000038 * TEST CASE TOTAL : 1
plars865695b2001-08-27 22:15:12 +000039 * CPU TYPES : i386
plars865695b2001-08-27 22:15:12 +000040 * AUTHORS : Ulrich Drepper
41 * Nate Straz
42 *
robbiew5e83f222003-05-07 15:46:32 +000043 *On Friday, May 2, 2003 at 09:47:00AM MST, Ulrich Drepper wrote:
44 *>Robert Williamson wrote:
45 *>
46 *>> I'm getting a SIGSEGV with one of our tests, fork05.c, that apparently
47 *>> you wrote (attached below). The test passes on my 2.5.68 machine running
48 *>> SuSE 8.0 (glibc 2.2.5 and Linuxthreads), however it segmentation faults on
49 *>> RedHat 9 running 2.5.68. The test seems to "break" when it attempts to run
50 *>> the assembly code....could you take a look at it?
51 *>
52 *>There is no need to look at it, I know it cannot work anymore on recent
53 *>systems. Either change all uses of %gs to %fs or skip the entire patch
54 *>if %gs has a nonzero value.
55 *>
subrata_modak4bb656a2009-02-26 12:02:09 +000056 *>- --
robbiew5e83f222003-05-07 15:46:32 +000057 *>- --------------. ,-. 444 Castro Street
58 *>Ulrich Drepper \ ,-----------------' \ Mountain View, CA 94041 USA
59 *>Red Hat `--' drepper at redhat.com `---------------------------
60 *
61 *
62 *
plars865695b2001-08-27 22:15:12 +000063 *On Sat, Aug 12, 2000 at 12:47:31PM -0700, Ulrich Drepper wrote:
64 *> Ever since the %gs handling was fixed in the 2.3.99 series the
65 *> appended test program worked. Now with 2.4.0-test6 it's not working
66 *> again. Looking briefly over the patch from test5 to test6 I haven't
67 *> seen an immediate candidate for the breakage. It could be missing
68 *> propagation of the LDT to the new process (and therefore an invalid
69 *> segment descriptor) or simply clearing %gs.
subrata_modak4bb656a2009-02-26 12:02:09 +000070 *>
plars865695b2001-08-27 22:15:12 +000071 *> Anyway, this is what you should see and what you get with test5:
subrata_modak4bb656a2009-02-26 12:02:09 +000072 *>
plars865695b2001-08-27 22:15:12 +000073 *> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74 *> a = 42
75 *> %gs = 0x0007
76 *> %gs = 0x0007
77 *> a = 99
78 *> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subrata_modak4bb656a2009-02-26 12:02:09 +000079 *>
plars865695b2001-08-27 22:15:12 +000080 *> This is what you get with test6:
subrata_modak4bb656a2009-02-26 12:02:09 +000081 *>
plars865695b2001-08-27 22:15:12 +000082 *> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83 *> a = 42
84 *> %gs = 0x0007
85 *> %gs = 0x0000
86 *> <SEGFAULT>
87 *> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subrata_modak4bb656a2009-02-26 12:02:09 +000088 *>
plars865695b2001-08-27 22:15:12 +000089 *> If somebody is actually creating a test suite for the kernel, please
90 *> add this program. It's mostly self-contained. The correct handling
91 *> of %gs is really important since glibc 2.2 will make heavy use of it.
subrata_modak4bb656a2009-02-26 12:02:09 +000092 *>
93 *> - --
plars865695b2001-08-27 22:15:12 +000094 *> - ---------------. ,-. 1325 Chesapeake Terrace
95 *> Ulrich Drepper \ ,-------------------' \ Sunnyvale, CA 94089 USA
96 *> Red Hat `--' drepper at redhat.com `------------------------
subrata_modak4bb656a2009-02-26 12:02:09 +000097 *>
plars865695b2001-08-27 22:15:12 +000098 *> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99 *
Wanlong Gao18190692012-09-18 17:14:01 +0800100 */
101
plars865695b2001-08-27 22:15:12 +0000102#include <stdio.h>
plars865695b2001-08-27 22:15:12 +0000103#include <fcntl.h>
104#include <unistd.h>
105#include <stdlib.h>
106#include <sys/wait.h>
robbiew4e20ed92002-06-10 15:02:59 +0000107#include "test.h"
robbiew4e20ed92002-06-10 15:02:59 +0000108
subrata_modak56207ce2009-03-23 13:35:39 +0000109char *TCID = "fork05";
robbiew4e20ed92002-06-10 15:02:59 +0000110
Wanlong Gao18190692012-09-18 17:14:01 +0800111static char *environ_list[] = { "TERM", "NoTSetzWq", "TESTPROG" };
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112
Wanlong Gao18190692012-09-18 17:14:01 +0800113#define NUMBER_OF_ENVIRON (sizeof(environ_list)/sizeof(char *))
114int TST_TOTAL = NUMBER_OF_ENVIRON;
robbiew4e20ed92002-06-10 15:02:59 +0000115
116#if defined(linux) && defined(__i386__)
plars865695b2001-08-27 22:15:12 +0000117
subrata_modak56207ce2009-03-23 13:35:39 +0000118struct modify_ldt_ldt_s {
119 unsigned int entry_number;
120 unsigned long int base_addr;
121 unsigned int limit;
122 unsigned int seg_32bit:1;
123 unsigned int contents:2;
124 unsigned int read_exec_only:1;
125 unsigned int limit_in_pages:1;
126 unsigned int seg_not_present:1;
127 unsigned int useable:1;
128 unsigned int empty:25;
plars865695b2001-08-27 22:15:12 +0000129};
130
Wanlong Gao18190692012-09-18 17:14:01 +0800131static int a = 42;
mridgef4f3d4f2005-01-05 21:21:27 +0000132
Wanlong Gao18190692012-09-18 17:14:01 +0800133static void modify_ldt(int, struct modify_ldt_ldt_s *, int);
mridgef4f3d4f2005-01-05 21:21:27 +0000134asm(" .text\n\
135 .type modify_ldt,@function \n\
plars74948ad2002-11-14 16:16:14 +0000136modify_ldt: \n\
137 push %ebx \n\
138 mov 0x10(%esp,1),%edx \n\
139 mov 0xc(%esp,1),%ecx \n\
140 mov 0x8(%esp,1),%ebx \n\
141 mov $0x7b,%eax \n\
142 int $0x80 \n\
143 pop %ebx \n\
plars865695b2001-08-27 22:15:12 +0000144 ret");
145
Mike Frysingerc57fba52014-04-09 18:56:30 -0400146int main(void)
plars865695b2001-08-27 22:15:12 +0000147{
subrata_modak56207ce2009-03-23 13:35:39 +0000148 struct modify_ldt_ldt_s ldt0;
149 int lo;
150 pid_t pid;
151 int res;
plars865695b2001-08-27 22:15:12 +0000152
subrata_modak56207ce2009-03-23 13:35:39 +0000153 ldt0.entry_number = 0;
154 ldt0.base_addr = (long)&a;
155 ldt0.limit = 4;
156 ldt0.seg_32bit = 1;
157 ldt0.contents = 0;
158 ldt0.read_exec_only = 0;
159 ldt0.limit_in_pages = 0;
160 ldt0.seg_not_present = 0;
161 ldt0.useable = 1;
162 ldt0.empty = 0;
plars865695b2001-08-27 22:15:12 +0000163
subrata_modak56207ce2009-03-23 13:35:39 +0000164 modify_ldt(1, &ldt0, sizeof(ldt0));
plars865695b2001-08-27 22:15:12 +0000165
Wanlong Gao18190692012-09-18 17:14:01 +0800166 asm volatile ("movw %w0, %%fs"::"q" (7));
plars865695b2001-08-27 22:15:12 +0000167
Wanlong Gao18190692012-09-18 17:14:01 +0800168 asm volatile ("movl %%fs:0, %0":"=r" (lo));
169 tst_resm(TINFO, "a = %d", lo);
plars865695b2001-08-27 22:15:12 +0000170
Wanlong Gao18190692012-09-18 17:14:01 +0800171 asm volatile ("pushl %%fs; popl %0":"=q" (lo));
172 tst_resm(TINFO, "%%fs = %#06hx", lo);
plars865695b2001-08-27 22:15:12 +0000173
Wanlong Gao18190692012-09-18 17:14:01 +0800174 asm volatile ("movl %0, %%fs:0"::"r" (99));
plars865695b2001-08-27 22:15:12 +0000175
Wanlong Gao18190692012-09-18 17:14:01 +0800176 pid = fork();
plars865695b2001-08-27 22:15:12 +0000177
subrata_modak56207ce2009-03-23 13:35:39 +0000178 if (pid == 0) {
Wanlong Gao18190692012-09-18 17:14:01 +0800179 asm volatile ("pushl %%fs; popl %0":"=q" (lo));
subrata_modak56207ce2009-03-23 13:35:39 +0000180 tst_resm(TINFO, "%%fs = %#06hx", lo);
plars865695b2001-08-27 22:15:12 +0000181
Wanlong Gao18190692012-09-18 17:14:01 +0800182 asm volatile ("movl %%fs:0, %0":"=r" (lo));
subrata_modak56207ce2009-03-23 13:35:39 +0000183 tst_resm(TINFO, "a = %d", lo);
plars865695b2001-08-27 22:15:12 +0000184
subrata_modak56207ce2009-03-23 13:35:39 +0000185 if (lo != 99)
186 tst_resm(TFAIL, "Test failed");
187 else
188 tst_resm(TPASS, "Test passed");
189 exit(lo != 99);
190 } else {
191 waitpid(pid, &res, 0);
192 }
plars865695b2001-08-27 22:15:12 +0000193
subrata_modak56207ce2009-03-23 13:35:39 +0000194 return WIFSIGNALED(res);
plars865695b2001-08-27 22:15:12 +0000195}
196
197#else /* if defined(linux) && defined(__i386__) */
198
Mike Frysingerc57fba52014-04-09 18:56:30 -0400199int main(void)
plars865695b2001-08-27 22:15:12 +0000200{
subrata_modak56207ce2009-03-23 13:35:39 +0000201 tst_resm(TINFO, "%%fs test only for ix86");
plars865695b2001-08-27 22:15:12 +0000202
subrata_modak56207ce2009-03-23 13:35:39 +0000203 /*
204 * should be successful on all non-ix86 platforms.
205 */
Garrett Cooper2c282152010-12-16 00:55:50 -0800206 tst_exit();
plars865695b2001-08-27 22:15:12 +0000207}
208
Wanlong Gao18190692012-09-18 17:14:01 +0800209#endif /* if defined(linux) && defined(__i386__) */