aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/socket.h
blob: a2fada9becb60c47fcbf8143b3b3c67d291731b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#ifndef _LINUX_SOCKET_H
#define _LINUX_SOCKET_H

/*
 * Desired design of maximum size and alignment (see RFC2553)
 */
#define _K_SS_MAXSIZE	128	/* Implementation specific max size */
#define _K_SS_ALIGNSIZE	(__alignof__ (struct sockaddr *))
				/* Implementation specific desired alignment */

struct __kernel_sockaddr_storage {
	unsigned short	ss_family;		/* address family */
	/* Following field(s) are implementation specific */
	char		__data[_K_SS_MAXSIZE - sizeof(unsigned short)];
				/* space to achieve desired size, */
				/* _SS_MAXSIZE value minus size of ss_family */
} __attribute__ ((aligned(_K_SS_ALIGNSIZE)));	/* force desired alignment */

#ifdef __KERNEL__

#include <asm/socket.h>			/* arch-dependent defines	*/
#include <linux/sockios.h>		/* the SIOCxxx I/O controls	*/
#include <linux/uio.h>			/* iovec support		*/
#include <linux/types.h>		/* pid_t			*/
#include <linux/compiler.h>		/* __user			*/

struct pid;
struct cred;

#define __sockaddr_check_size(size)	\
	BUILD_BUG_ON(((size) > sizeof(struct __kernel_sockaddr_storage)))

#ifdef __KERNEL__
# ifdef CONFIG_PROC_FS
struct seq_file;
extern void socket_seq_show(struct seq_file *seq);
# endif
#endif /* __KERNEL__ */

typedef unsigned short	sa_family_t;

/*
 *	1003.1g requires sa_family_t and that sa_data is char.
 */
 
struct sockaddr {
	sa_family_t	sa_family;	/* address family, AF_xxx	*/
	char		sa_data[14];	/* 14 bytes of protocol address	*/
};

struct linger {
	int		l_onoff;	/* Linger active		*/
	int		l_linger;	/* How long to linger for	*/
};

#define sockaddr_storage __kernel_sockaddr_storage

/*
 *	As we do 4.4BSD message passing we use a 4.4BSD message passing
 *	system, not 4.3. Thus msg_accrights(len) are now missing. They
 *	belong in an obscure libc emulation or the bin.
 */
 
struct msghdr {
	void	*	msg_name;	/* Socket name			*/
	int		msg_namelen;	/* Length of name		*/
	struct iovec *	msg_iov;	/* Data blocks			*/
	__kernel_size_t	msg_iovlen;	/* Number of blocks		*/
	void 	*	msg_control;	/* Per protocol magic (eg BSD file descriptor passing) */
	__kernel_size_t	msg_controllen;	/* Length of cmsg list */
	unsigned	msg_flags;
};

/* For recvmmsg/sendmmsg */
struct mmsghdr {
	struct msghdr   msg_hdr;
	unsigned        msg_len;
};

/*
 *	POSIX 1003.1g - ancillary data object information
 *	Ancillary data consits of a sequence of pairs of
 *	(cmsghdr, cmsg_data[])
 */

struct cmsghdr {
	__kernel_size_t	cmsg_len;	/* data byte count, including hdr */
        int		cmsg_level;	/* originating protocol */
        int		cmsg_type;	/* protocol-specific type */
};

/*
 *	Ancilliary data object information MACROS
 *	Table 5-14 of POSIX 1003.1g
 */

#define __CMSG_NXTHDR(ctl, len, cmsg) __cmsg_nxthdr((ctl),(len),(cmsg))
#define CMSG_NXTHDR(mhdr, cmsg) cmsg_nxthdr((mhdr), (cmsg))

#define CMSG_ALIGN(len) ( ((len)+sizeof(long)-1) & ~(sizeof(long)-1) )

#define CMSG_DATA(cmsg)	((void *)((char *)(cmsg) + CMSG_ALIGN(sizeof(struct cmsghdr))))
#define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
#define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))

#define __CMSG_FIRSTHDR(ctl,len) ((len) >= sizeof(struct cmsghdr) ? \
				  (struct cmsghdr *)(ctl) : \
				  (struct cmsghdr *)NULL)
#define CMSG_FIRSTHDR(msg)	__CMSG_FIRSTHDR((msg)->msg_control, (msg)->msg_controllen)
#define CMSG_OK(mhdr, cmsg) ((cmsg)->cmsg_len >= sizeof(struct cmsghdr) && \
			     (cmsg)->cmsg_len <= (unsigned long) \
			     ((mhdr)->msg_controllen - \
			      ((char *)(cmsg) - (char *)(mhdr)->msg_control)))

/*
 *	Get the next cmsg header
 *
 *	PLEASE, do not touch this function. If you think, that it is
 *	incorrect, grep kernel sources and think about consequences
 *	before trying to improve it.
 *
 *	Now it always returns valid, not truncated ancillary object
 *	HEADER. But caller still MUST check, that cmsg->cmsg_len is
 *	inside range, given by msg->msg_controllen before using
 *	ancillary object DATA.				--ANK (980731)
 */
 
static inline struct cmsghdr * __cmsg_nxthdr(void *__ctl, __kernel_size_t __size,
					       struct cmsghdr *__cmsg)
{
	struct cmsghdr * __ptr;

	__ptr = (struct cmsghdr*)(((unsigned char *) __cmsg) +  CMSG_ALIGN(__cmsg->cmsg_len));
	if ((unsigned long)((char*)(__ptr+1) - (char *) __ctl) > __size)
		return (struct cmsghdr *)0;

	return __ptr;
}

static inline struct cmsghdr * cmsg_nxthdr (struct msghdr *__msg, struct cmsghdr *__cmsg)
{
	return __cmsg_nxthdr(__msg->msg_control, __msg->msg_controllen, __cmsg);
}

/* "Socket"-level control message types: */

#define	SCM_RIGHTS	0x01		/* rw: access rights (array of int) */
#define SCM_CREDENTIALS 0x02		/* rw: struct ucred		*/
#define SCM_SECURITY	0x03		/* rw: security label		*/

struct ucred {
	__u32	pid;
	__u32	uid;
	__u32	gid;
};

/* Supported address families. */
#define AF_UNSPEC	0
#define AF_UNIX		1	/* Unix domain sockets 		*/
#define AF_LOCAL	1	/* POSIX name for AF_UNIX	*/
#define AF_INET		2	/* Internet IP Protocol 	*/
#define AF_AX25		3	/* Amateur Radio AX.25 		*/
#define AF_IPX		4	/* Novell IPX 			*/
#define AF_APPLETALK	5	/* AppleTalk DDP 		*/
#define AF_NETROM	6	/* Amateur Radio NET/ROM 	*/
#define AF_BRIDGE	7	/* Multiprotocol bridge 	*/
#define AF_ATMPVC	8	/* ATM PVCs			*/
#define AF_X25		9	/* Reserved for X.25 project 	*/
#define AF_INET6	10	/* IP version 6			*/
#define AF_ROSE		11	/* Amateur Radio X.25 PLP	*/
#define AF_DECnet	12	/* Reserved for DECnet project	*/
#define AF_NETBEUI	13	/* Reserved for 802.2LLC project*/
#define AF_SECURITY	14	/* Security callback pseudo AF */
#define AF_KEY		15      /* PF_KEY key management API */
#define AF_NETLINK	16
#define AF_ROUTE	AF_NETLINK /* Alias to emulate 4.4BSD */
#define AF_PACKET	17	/* Packet family		*/
#define AF_ASH		18	/* Ash				*/
#define AF_ECONET	19	/* Acorn Econet			*/
#define AF_ATMSVC	20	/* ATM SVCs			*/
#define AF_RDS		21	/* RDS sockets 			*/
#define AF_SNA		22	/* Linux SNA Project (nutters!) */
#define AF_IRDA		23	/* IRDA sockets			*/
#define AF_PPPOX	24	/* PPPoX sockets		*/
#define AF_WANPIPE	25	/* Wanpipe API Sockets */
#define AF_LLC		26	/* Linux LLC			*/
#define AF_CAN		29	/* Controller Area Network      */
#define AF_TIPC		30	/* TIPC sockets			*/
#define AF_BLUETOOTH	31	/* Bluetooth sockets 		*/
#define AF_IUCV		32	/* IUCV sockets			*/
#define AF_RXRPC	33	/* RxRPC sockets 		*/
#define AF_ISDN		34	/* mISDN sockets 		*/
#define AF_PHONET	35	/* Phonet sockets		*/
#define AF_IEEE802154	36	/* IEEE802154 sockets		*/
#define AF_CAIF		37	/* CAIF sockets			*/
#define AF_MAX		38	/* For now.. */

/* Protocol families, same as address families. */
#define PF_UNSPEC	AF_UNSPEC
#define PF_UNIX		AF_UNIX
#define PF_LOCAL	AF_LOCAL
#define PF_INET		AF_INET
#define PF_AX25		AF_AX25
#define PF_IPX		AF_IPX
#define PF_APPLETALK	AF_APPLETALK
#define	PF_NETROM	AF_NETROM
#define PF_BRIDGE	AF_BRIDGE
#define PF_ATMPVC	AF_ATMPVC
#define PF_X25		AF_X25
#define PF_INET6	AF_INET6
#define PF_ROSE		AF_ROSE
#define PF_DECnet	AF_DECnet
#define PF_NETBEUI	AF_NETBEUI
#define PF_SECURITY	AF_SECURITY
#define PF_KEY		AF_KEY
#define PF_NETLINK	AF_NETLINK
#define PF_ROUTE	AF_ROUTE
#define PF_PACKET	AF_PACKET
#define PF_ASH		AF_ASH
#define PF_ECONET	AF_ECONET
#define PF_ATMSVC	AF_ATMSVC
#define PF_RDS		AF_RDS
#define PF_SNA		AF_SNA
#define PF_IRDA		AF_IRDA
#define PF_PPPOX	AF_PPPOX
#define PF_WANPIPE	AF_WANPIPE
#define PF_LLC		AF_LLC
#define PF_CAN		AF_CAN
#define PF_TIPC		AF_TIPC
#define PF_BLUETOOTH	AF_BLUETOOTH
#define PF_IUCV		AF_IUCV
#define PF_RXRPC	AF_RXRPC
#define PF_ISDN		AF_ISDN
#define PF_PHONET	AF_PHONET
#define PF_IEEE802154	AF_IEEE802154
#define PF_CAIF		AF_CAIF
#define PF_MAX		AF_MAX

/* Maximum queue length specifiable by listen.  */
#define SOMAXCONN	128

/* Flags we can use with send/ and recv. 
   Added those for 1003.1g not all are supported yet
 */
 
#define MSG_OOB		1
#define MSG_PEEK	2
#define MSG_DONTROUTE	4
#define MSG_TRYHARD     4       /* Synonym for MSG_DONTROUTE for DECnet */
#define MSG_CTRUNC	8
#define MSG_PROBE	0x10	/* Do not send. Only probe path f.e. for MTU */
#define MSG_TRUNC	0x20
#define MSG_DONTWAIT	0x40	/* Nonblocking io		 */
#define MSG_EOR         0x80	/* End of record */
#define MSG_WAITALL	0x100	/* Wait for a full request */
#define MSG_FIN         0x200
#define MSG_SYN		0x400
#define MSG_CONFIRM	0x800	/* Confirm path validity */
#define MSG_RST		0x1000
#define MSG_ERRQUEUE	0x2000	/* Fetch message from error queue */
#define MSG_NOSIGNAL	0x4000	/* Do not generate SIGPIPE */
#define MSG_MORE	0x8000	/* Sender will send more */
#define MSG_WAITFORONE	0x10000	/* recvmmsg(): block until 1+ packets avail */

#define MSG_EOF         MSG_FIN

#define MSG_CMSG_CLOEXEC 0x40000000	/* Set close_on_exit for file
					   descriptor received through
					   SCM_RIGHTS */
#if defined(CONFIG_COMPAT)
#define MSG_CMSG_COMPAT	0x80000000	/* This message needs 32 bit fixups */
#else
#define MSG_CMSG_COMPAT	0		/* We never have 32 bit fixups */
#endif


/* Setsockoptions(2) level. Thanks to BSD these must match IPPROTO_xxx */
#define SOL_IP		0
/* #define SOL_ICMP	1	No-no-no! Due to Linux :-) we cannot use SOL_ICMP=1 */
#define SOL_TCP		6
#define SOL_UDP		17
#define SOL_IPV6	41
#define SOL_ICMPV6	58
#define SOL_SCTP	132
#define SOL_UDPLITE	136     /* UDP-Lite (RFC 3828) */
#define SOL_RAW		255
#define SOL_IPX		256
#define SOL_AX25	257
#define SOL_ATALK	258
#define SOL_NETROM	259
#define SOL_ROSE	260
#define SOL_DECNET	261
#define	SOL_X25		262
#define SOL_PACKET	263
#define SOL_ATM		264	/* ATM layer (cell level) */
#define SOL_AAL		265	/* ATM Adaption Layer (packet level) */
#define SOL_IRDA        266
#define SOL_NETBEUI	267
#define SOL_LLC		268
#define SOL_DCCP	269
#define SOL_NETLINK	270
#define SOL_TIPC	271
#define SOL_RXRPC	272
#define SOL_PPPOL2TP	273
#define SOL_BLUETOOTH	274
#define SOL_PNPIPE	275
#define SOL_RDS		276
#define SOL_IUCV	277
#define SOL_CAIF	278

/* IPX options */
#define IPX_TYPE	1

#ifdef __KERNEL__
extern void cred_to_ucred(struct pid *pid, const struct cred *cred, struct ucred *ucred);

extern int memcpy_fromiovec(unsigned char *kdata, struct iovec *iov, int len);
extern int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
			       int offset, int len);
extern int csum_partial_copy_fromiovecend(unsigned char *kdata, 
					  struct iovec *iov, 
					  int offset, 
					  unsigned int len, __wsum *csump);

extern int verify_iovec(struct msghdr *m, struct iovec *iov, struct sockaddr *address, int mode);
extern int memcpy_toiovec(struct iovec *v, unsigned char *kdata, int len);
extern int memcpy_toiovecend(const struct iovec *v, unsigned char *kdata,
			     int offset, int len);
extern int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr, int __user *ulen);
extern int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr);
extern int put_cmsg(struct msghdr*, int level, int type, int len, void *data);

struct timespec;

extern int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
			  unsigned int flags, struct timespec *timeout);
#endif
#endif /* not kernel and not glibc */
#endif /* _LINUX_SOCKET_H */
v>
4c61afcdb2cd





7bfb72e847c2
4c61afcdb2cd

57a6a46aa26d

9ae2847591c8

d75586ad01e6

2171787be2e7
d75586ad01e6


2171787be2e7
d75586ad01e6
2171787be2e7
d75586ad01e6

d75586ad01e6





9ae2847591c8









d75586ad01e6




9ae2847591c8
d75586ad01e6


d7c8f21a8cad
ed724be65fa1




c31c7d4844ea

ed724be65fa1


687c4825b6cc
ed724be65fa1

687c4825b6cc
c31c7d4844ea
ed724be65fa1



c31c7d4844ea

ed724be65fa1


cc0f21bbc12d
cc0f21bbc12d
c31c7d4844ea

cc0f21bbc12d
c31c7d4844ea

cc0f21bbc12d
ed724be65fa1

687c4825b6cc



9a14aefc1d28







da7bfc50f5cb
9f4c815ce7ab
1da177e4c3f4


9f4c815ce7ab
30551bb3ce92

1da177e4c3f4

9df84993cb3d
1da177e4c3f4


c2f71ee2140b




1da177e4c3f4


30551bb3ce92

9a14aefc1d28
1da177e4c3f4
1da177e4c3f4
30551bb3ce92
9df84993cb3d
9f4c815ce7ab

75bb88350e05
9f4c815ce7ab
9df84993cb3d


9a3dc7804e98
9f4c815ce7ab
9f4c815ce7ab

44af6c41e6a0
e4b71dcf54fa
44af6c41e6a0

e3ed910db221
44af6c41e6a0








1da177e4c3f4
44af6c41e6a0
1da177e4c3f4

9df84993cb3d


65e074dffa19
c31c7d4844ea
65e074dffa19

fac84939609a
da7bfc50f5cb
65e074dffa19
c9caa02c529d


65e074dffa19










31422c51e0dc

65e074dffa19
f07333fd149e
65e074dffa19
5d3c8b21e227

f07333fd149e

65e074dffa19
beaff6333b4a
65e074dffa19








9b5cf48b06a5

65e074dffa19








c31c7d4844ea








65e074dffa19

fac84939609a




c31c7d4844ea
9b5cf48b06a5
c31c7d4844ea
fac84939609a





65e074dffa19



beaff6333b4a
65e074dffa19










9b5cf48b06a5
65e074dffa19





d75586ad01e6
beaff6333b4a
65e074dffa19



9df84993cb3d
beaff6333b4a
65e074dffa19

7afe15b9d888
bb5c2dbd57d9
7b610eec7a06
9df84993cb3d
bb5c2dbd57d9
9df84993cb3d
ad5ca55f6bdb






8311eb84bf84

bb5c2dbd57d9
eb5b5f024c40
bb5c2dbd57d9




6ce9fc17d913
bb5c2dbd57d9

bb5c2dbd57d9
6944a9c89452
07cf89c05f2b
7a5714e01860






bb5c2dbd57d9
f07333fd149e



f07333fd149e


63c1dcf4bc9a



f07333fd149e
63c1dcf4bc9a
bb5c2dbd57d9
ce0c0e50f94e
f361a450bf1a




65280e613fad

f361a450bf1a
ce0c0e50f94e
bb5c2dbd57d9
07a66d7c53a5
4c881ca1819d
07a66d7c53a5


bb5c2dbd57d9
07a66d7c53a5
211b3d03c740










bb5c2dbd57d9


eb5b5f024c40



8311eb84bf84

9a3dc7804e98
bb5c2dbd57d9
bb5c2dbd57d9


a1e46212a410





























c31c7d4844ea
9f4c815ce7ab
d75586ad01e6
da7bfc50f5cb

c31c7d4844ea
1da177e4c3f4
9ae2847591c8


d75586ad01e6


97f99fedf27f
f0646e43acb1
1da177e4c3f4
a1e46212a410
c31c7d4844ea

a1e46212a410

9f4c815ce7ab
30551bb3ce92
c31c7d4844ea
626c2c9d065d
c31c7d4844ea
86f03989d99c
72e458dfa63b

86f03989d99c
c31c7d4844ea
86f03989d99c
626c2c9d065d




c31c7d4844ea

f4ae5da0e8e9




d75586ad01e6
f4ae5da0e8e9
9b5cf48b06a5
65e074dffa19
1da177e4c3f4
65e074dffa19




beaff6333b4a
65e074dffa19

9b5cf48b06a5
65e074dffa19

87f7f8fe3283

65e074dffa19



87f7f8fe3283

ad5ca55f6bdb


















87f7f8fe3283

beaff6333b4a
87f7f8fe3283
9f4c815ce7ab
1da177e4c3f4
c31c7d4844ea


1da177e4c3f4
c31c7d4844ea
f34b439f34c4
d75586ad01e6
44af6c41e6a0
965194c15dc9
c31c7d4844ea
626c2c9d065d
f361a450bf1a
965194c15dc9
f361a450bf1a

f34b439f34c4



9ae2847591c8


d75586ad01e6




a1e46212a410
44af6c41e6a0
f34b439f34c4
d75586ad01e6

9ae2847591c8
d75586ad01e6
f34b439f34c4


44af6c41e6a0
44af6c41e6a0
c31c7d4844ea

488fd99588bf
f34b439f34c4


93dbda7cbcd7
f34b439f34c4


0879750f5d75

488fd99588bf
c31c7d4844ea

0879750f5d75
c31c7d4844ea
d75586ad01e6

9ae2847591c8
c31c7d4844ea




488fd99588bf
c31c7d4844ea
1da177e4c3f4

c31c7d4844ea
ff31452b6ea5
65e074dffa19
ff31452b6ea5
65e074dffa19




9b5cf48b06a5
d75586ad01e6
9ae2847591c8
d75586ad01e6
c31c7d4844ea
ad5ca55f6bdb

c31c7d4844ea
ad5ca55f6bdb

ff31452b6ea5

ff31452b6ea5
c31c7d4844ea





65e074dffa19




9b5cf48b06a5

9ae2847591c8
d75586ad01e6



65e074dffa19
ff31452b6ea5


6bb8383bebc0





d75586ad01e6
c9caa02c529d
9ae2847591c8

ff31452b6ea5
72e458dfa63b
cacf890694a3
331e406588dc






c9caa02c529d
331e406588dc

69b1415e934f
9ae2847591c8
d75586ad01e6






9ae2847591c8











69b1415e934f

5843d9a4d0ba


db64fe02258f

72e458dfa63b
9ae2847591c8
72e458dfa63b


d75586ad01e6

c9caa02c529d
72e458dfa63b
9ae2847591c8

d75586ad01e6
af96e4438a4b



ff31452b6ea5
57a6a46aa26d
f4ae5da0e8e9

d75586ad01e6
1ac2f7d55b7e
cacf890694a3
f4ae5da0e8e9
6bb8383bebc0





57a6a46aa26d

af1e6844d600
57a6a46aa26d

d75586ad01e6
9ae2847591c8



d75586ad01e6

6bb8383bebc0
cacf890694a3
76ebd0548df6
ff31452b6ea5


d75586ad01e6

75cbade8ea31
d75586ad01e6
9ae2847591c8
75cbade8ea31

d75586ad01e6

72932c7ad2cc
d75586ad01e6
9ae2847591c8
72932c7ad2cc

0f3507555f6f













1219333dfdd4
72932c7ad2cc
de33c442ed2a


d75586ad01e6

75cbade8ea31
1219333dfdd4


9fa3ab390abf

de33c442ed2a


9fa3ab390abf









1219333dfdd4
9fa3ab390abf



1219333dfdd4
75cbade8ea31

d75586ad01e6

9fa3ab390abf


d75586ad01e6



9fa3ab390abf



d75586ad01e6

9fa3ab390abf
d75586ad01e6
9fa3ab390abf









d75586ad01e6


ef354af4629e

3869c4aa1883





d75586ad01e6
3869c4aa1883

ef354af4629e



9fa3ab390abf

499f8f84b832
ef354af4629e

9fa3ab390abf



ef354af4629e
9fa3ab390abf









ef354af4629e


1219333dfdd4
75cbade8ea31
d75586ad01e6

75cbade8ea31
1219333dfdd4


9fa3ab390abf





c15238df3b65
9fa3ab390abf
1219333dfdd4
75cbade8ea31

d75586ad01e6


a5593e0b329a



9fa3ab390abf

d75586ad01e6
9fa3ab390abf

c5e147cf5aeb
9fa3ab390abf
d75586ad01e6


75cbade8ea31

d75586ad01e6
75cbade8ea31




d75586ad01e6
75cbade8ea31




d75586ad01e6
75cbade8ea31
a03352d2c1dc
75cbade8ea31


d75586ad01e6
75cbade8ea31
a03352d2c1dc
f62d0f008e88


d75586ad01e6
f62d0f008e88
75cbade8ea31
c9caa02c529d

d75586ad01e6
9ae2847591c8
c9caa02c529d

75cbade8ea31


75cbade8ea31
d7c8f21a8cad
75cbade8ea31


0f3507555f6f




























75cbade8ea31


75cbade8ea31
d7c8f21a8cad
75cbade8ea31


0f3507555f6f








9fa3ab390abf

0f3507555f6f






9fa3ab390abf
0f3507555f6f


75cbade8ea31


75cbade8ea31
d7c8f21a8cad
75cbade8ea31





75cbade8ea31
d7c8f21a8cad
75cbade8ea31





75cbade8ea31
d7c8f21a8cad
75cbade8ea31
75cbade8ea31



e81d5dc41b67
d7c8f21a8cad
78c94abaea55

1da177e4c3f4
f62d0f008e88


d75586ad01e6

72e458dfa63b

d75586ad01e6

72932c7ad2cc
55121b4369ce






f62d0f008e88



d75586ad01e6

72e458dfa63b

d75586ad01e6

72932c7ad2cc
55121b4369ce






f62d0f008e88

1da177e4c3f4



9f4c815ce7ab
f9b8404cf8f8

9f4c815ce7ab
de5097c2e73f
9f4c815ce7ab
12d6f21eacc2





f8d8406bcb58
55121b4369ce

1da177e4c3f4
f62d0f008e88



9f4c815ce7ab

e4b71dcf54fa

1da177e4c3f4

ee7ae7a1981c

8a235efad548
















d1028a154c65







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282

                                        
                                               
   
                          
                          
                         
                        
                       
                     
                            

                           
 
                     

                          
                         
                      

                        
                      
                    
 


                                                                    
                 
                               

                                 
                                 
                              
                            
                                        
                                
                                

  







                                                                             

                      
                         
 
                     

                                                      
                                                      
 
                            
 



                                                 







                                                      
                                            
 
                                                  

                                                              
                                                  

                                                               
                                                  

                                                               
                    
                           
                                                          
                                                               

      


                                                  
 








                                                   
                                                               



      





                             

                                                                  
 



                                           

                     
 







                                                                      
                                                        
 
                                      
 
             







                                                                     
             

 
                                      
 

                                                 





                                                                     
                                            


                         
                                              


                                
                                                        

 

                                        





                                                                     

 
                                                                         
 


                              
                                
                                            
 
                                                
 


                       





                                                      





                                                                         
                                                           

                                                                      

 

                                                                          

                              
                                                                               


                                
                                                            
 
                                

                       





                                                      









                                                                     




                                                           
                                                                     


         
  




                                                                           

                                                                               


                                         
          

                                                                        
           
                                                                          



                                                                     

                                                                    


                                                                         
 
          

                                                                   
           

                                                                          
                                                  

                                                                   



                    







                                                                      
                                                                 
 


                                           
 

                               

                            
 


                                       




                                                  


                                       

                             
                                                  
                                    
 
                             
 

                                               
                                  
 


                                                 
                                                                        
 

                                  
                    
                                 

                                  
                                                           








                                                                               
         
      

 


                                                           
 
                                                                              

                                     
                            
                           
 


                             










                                                                  

                                      
                      
                    
                         

                                      

                      
                
                                   








                                                                   

                                         








                                                                     








                                                                         

          




                                                                     
              
                                                                       
                                                                            





                                                                 



                                                                     
                             










                                                                    
                                                                              





                                                                            
                                           
                             



                                                 
 
                        

 
                                                               
 
                                             
                              
                           
                          






                                          

                               
 
                                            




                                                                  
                        

                                
                                            
                                                        
                                                  






                                                                
 



                                                     


         



                                                      
                                                         
                                                           
 
                                                




                                                                                

                                                                            
      
 
          
                                               
          


                                                                       
           
                                                                            










                                                                       


                    



                                                               

                                  
                                                 
 


                 





























                                                                               
                                                                
 
                              

                           
                             
 


                                                                                


                                                   
       
                                               
                  
                                                                  

                        

                                                                  
 
                                   
                              
                                                        
                                                     
 

                                                                   
 
                                                                      
 




                                                                       

                                                               




                                                           
                                                   
                 
                                  
                         
         




                                                           
                                                               

                                                            
                                                                      

                          

                                



                                           

                                              


















                                                                                

                            
 
                   
 
 


                                                                            
 
                                  
                    
                                            
 
                                       
                         
 
                    
                                                                                

                         



                                                                    


                                                                              




                                                 
                                                                     
 
                                 

                                                                              
                                                                  
 


                                                                
 
                    

                           
          


                                                                  
                                                           


                         

                                                                    
           

                                                                      
 
                         

                                                                                   
                                                          




                                                                           
      
                   

 
                                                                           
 
                                          
 




                                                                     
                                         
                                                                
                                                               
                                          
 

                                             
                                                          

                                               

                                   
 





                                                     




                                                                    

                                                 
                                                               



                                                                 
         


                 





                                                                      
                                                                      
                                                                         

                                                                 
 
                            
                                   






                                                               
                                                                           

                         
                                             
                                  






                                                     











                                                                               

         


                                                              

                           
                         
                          


                                

                        
                                      
 

                                                    
 



                                                                               
 
          

                                                     
                                        
                         
 
          





                                                                   

                                                                      
                                                               

                   
                                      



                                                                

                                                                
                                     
 
    


                   

                                                                         
 
                                                                             
                                               

 

                                                                           
 
                                                                             
                                               

 













                                                                             
                                                    
 


                                                              

                                                                       
 


                                                   

                


                                                              









                                                                            
 



                                                                    
 

                             

                                                             


                 



                                                              



                                                                               

         
                                                     
                                                                       









                                                                       


                                   

                                                    





                                                                       
                                                                 

                   



                                                   

                
                         

                                                     



                                                                            
 









                                                                    


                             
                                                    
 

                                                                     
 


                                                   





                                             
                                                                    
                 
 

                             


                                                             



                                                                     

                           
 

                                                                       
 
                 


                                   

                                                  
                                                                              




                                                   
                                                                            




                                                   
                                                                              
 
                                 


                                                   
                                                                            
 
                                 


                                                   
                                                                                   
 
 

                                                   
                                                                     
                                                                 

 


                                                               
 
                                             


                            




























                                                                            


                                                               
 
                                             


                            








                                                            

                              






                                                              
                 


                                  


                                                               
 
                                            





                                                               
 
                                             





                                                               
 
                                             
 



                                                               
 
                                             

 
                             


                                                         

                                                                    

                                                                               

                                                        
 






                                                                        



                                                          

                                                                    

                                                        

                                                                               
 






                                                                            

 



                                                                  
                      

                                                                 
         
 
          





                                                                    
                                                                

                                                                      
           



                                               

          

                                                         

                          

 
















                                                                        







                                                                            
/*
 * Copyright 2002 Andi Kleen, SuSE Labs.
 * Thanks to Ben LaHaise for precious feedback.
 */
#include <linux/highmem.h>
#include <linux/bootmem.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/seq_file.h>
#include <linux/debugfs.h>

#include <asm/e820.h>
#include <asm/processor.h>
#include <asm/tlbflush.h>
#include <asm/sections.h>
#include <asm/setup.h>
#include <asm/uaccess.h>
#include <asm/pgalloc.h>
#include <asm/proto.h>
#include <asm/pat.h>

/*
 * The current flushing context - we pass it instead of 5 arguments:
 */
struct cpa_data {
	unsigned long	*vaddr;
	pgprot_t	mask_set;
	pgprot_t	mask_clr;
	int		numpages;
	int		flags;
	unsigned long	pfn;
	unsigned	force_split : 1;
	int		curpage;
	struct page	**pages;
};

/*
 * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
 * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
 * entries change the page attribute in parallel to some other cpu
 * splitting a large page entry along with changing the attribute.
 */
static DEFINE_SPINLOCK(cpa_lock);

#define CPA_FLUSHTLB 1
#define CPA_ARRAY 2
#define CPA_PAGES_ARRAY 4

#ifdef CONFIG_PROC_FS
static unsigned long direct_pages_count[PG_LEVEL_NUM];

void update_page_count(int level, unsigned long pages)
{
	unsigned long flags;

	/* Protect against CPA */
	spin_lock_irqsave(&pgd_lock, flags);
	direct_pages_count[level] += pages;
	spin_unlock_irqrestore(&pgd_lock, flags);
}

static void split_page_count(int level)
{
	direct_pages_count[level]--;
	direct_pages_count[level - 1] += PTRS_PER_PTE;
}

void arch_report_meminfo(struct seq_file *m)
{
	seq_printf(m, "DirectMap4k:    %8lu kB\n",
			direct_pages_count[PG_LEVEL_4K] << 2);
#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
	seq_printf(m, "DirectMap2M:    %8lu kB\n",
			direct_pages_count[PG_LEVEL_2M] << 11);
#else
	seq_printf(m, "DirectMap4M:    %8lu kB\n",
			direct_pages_count[PG_LEVEL_2M] << 12);
#endif
#ifdef CONFIG_X86_64
	if (direct_gbpages)
		seq_printf(m, "DirectMap1G:    %8lu kB\n",
			direct_pages_count[PG_LEVEL_1G] << 20);
#endif
}
#else
static inline void split_page_count(int level) { }
#endif

#ifdef CONFIG_X86_64

static inline unsigned long highmap_start_pfn(void)
{
	return __pa(_text) >> PAGE_SHIFT;
}

static inline unsigned long highmap_end_pfn(void)
{
	return __pa(roundup(_brk_end, PMD_SIZE)) >> PAGE_SHIFT;
}

#endif

#ifdef CONFIG_DEBUG_PAGEALLOC
# define debug_pagealloc 1
#else
# define debug_pagealloc 0
#endif

static inline int
within(unsigned long addr, unsigned long start, unsigned long end)
{
	return addr >= start && addr < end;
}

/*
 * Flushing functions
 */

/**
 * clflush_cache_range - flush a cache range with clflush
 * @addr:	virtual start address
 * @size:	number of bytes to flush
 *
 * clflush is an unordered instruction which needs fencing with mfence
 * to avoid ordering issues.
 */
void clflush_cache_range(void *vaddr, unsigned int size)
{
	void *vend = vaddr + size - 1;

	mb();

	for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
		clflush(vaddr);
	/*
	 * Flush any possible final partial cacheline:
	 */
	clflush(vend);

	mb();
}

static void __cpa_flush_all(void *arg)
{
	unsigned long cache = (unsigned long)arg;

	/*
	 * Flush all to work around Errata in early athlons regarding
	 * large page flushing.
	 */
	__flush_tlb_all();

	if (cache && boot_cpu_data.x86 >= 4)
		wbinvd();
}

static void cpa_flush_all(unsigned long cache)
{
	BUG_ON(irqs_disabled());

	on_each_cpu(__cpa_flush_all, (void *) cache, 1);
}

static void __cpa_flush_range(void *arg)
{
	/*
	 * We could optimize that further and do individual per page
	 * tlb invalidates for a low number of pages. Caveat: we must
	 * flush the high aliases on 64bit as well.
	 */
	__flush_tlb_all();
}

static void cpa_flush_range(unsigned long start, int numpages, int cache)
{
	unsigned int i, level;
	unsigned long addr;

	BUG_ON(irqs_disabled());
	WARN_ON(PAGE_ALIGN(start) != start);

	on_each_cpu(__cpa_flush_range, NULL, 1);

	if (!cache)
		return;

	/*
	 * We only need to flush on one CPU,
	 * clflush is a MESI-coherent instruction that
	 * will cause all other CPUs to flush the same
	 * cachelines:
	 */
	for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
		pte_t *pte = lookup_address(addr, &level);

		/*
		 * Only flush present addresses:
		 */
		if (pte && (pte_val(*pte) & _PAGE_PRESENT))
			clflush_cache_range((void *) addr, PAGE_SIZE);
	}
}

static void cpa_flush_array(unsigned long *start, int numpages, int cache,
			    int in_flags, struct page **pages)
{
	unsigned int i, level;
	unsigned long do_wbinvd = cache && numpages >= 1024; /* 4M threshold */

	BUG_ON(irqs_disabled());

	on_each_cpu(__cpa_flush_all, (void *) do_wbinvd, 1);

	if (!cache || do_wbinvd)
		return;

	/*
	 * We only need to flush on one CPU,
	 * clflush is a MESI-coherent instruction that
	 * will cause all other CPUs to flush the same
	 * cachelines:
	 */
	for (i = 0; i < numpages; i++) {
		unsigned long addr;
		pte_t *pte;

		if (in_flags & CPA_PAGES_ARRAY)
			addr = (unsigned long)page_address(pages[i]);
		else
			addr = start[i];

		pte = lookup_address(addr, &level);

		/*
		 * Only flush present addresses:
		 */
		if (pte && (pte_val(*pte) & _PAGE_PRESENT))
			clflush_cache_range((void *)addr, PAGE_SIZE);
	}
}

/*
 * Certain areas of memory on x86 require very specific protection flags,
 * for example the BIOS area or kernel text. Callers don't always get this
 * right (again, ioremap() on BIOS memory is not uncommon) so this function
 * checks and fixes these known static required protection bits.
 */
static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
				   unsigned long pfn)
{
	pgprot_t forbidden = __pgprot(0);

	/*
	 * The BIOS area between 640k and 1Mb needs to be executable for
	 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
	 */
	if (within(pfn, BIOS_BEGIN >> PAGE_SHIFT, BIOS_END >> PAGE_SHIFT))
		pgprot_val(forbidden) |= _PAGE_NX;

	/*
	 * The kernel text needs to be executable for obvious reasons
	 * Does not cover __inittext since that is gone later on. On
	 * 64bit we do not enforce !NX on the low mapping
	 */
	if (within(address, (unsigned long)_text, (unsigned long)_etext))
		pgprot_val(forbidden) |= _PAGE_NX;

	/*
	 * The .rodata section needs to be read-only. Using the pfn
	 * catches all aliases.
	 */
	if (within(pfn, __pa((unsigned long)__start_rodata) >> PAGE_SHIFT,
		   __pa((unsigned long)__end_rodata) >> PAGE_SHIFT))
		pgprot_val(forbidden) |= _PAGE_RW;

	prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));

	return prot;
}

/*
 * Lookup the page table entry for a virtual address. Return a pointer
 * to the entry and the level of the mapping.
 *
 * Note: We return pud and pmd either when the entry is marked large
 * or when the present bit is not set. Otherwise we would return a
 * pointer to a nonexisting mapping.
 */
pte_t *lookup_address(unsigned long address, unsigned int *level)
{
	pgd_t *pgd = pgd_offset_k(address);
	pud_t *pud;
	pmd_t *pmd;

	*level = PG_LEVEL_NONE;

	if (pgd_none(*pgd))
		return NULL;

	pud = pud_offset(pgd, address);
	if (pud_none(*pud))
		return NULL;

	*level = PG_LEVEL_1G;
	if (pud_large(*pud) || !pud_present(*pud))
		return (pte_t *)pud;

	pmd = pmd_offset(pud, address);
	if (pmd_none(*pmd))
		return NULL;

	*level = PG_LEVEL_2M;
	if (pmd_large(*pmd) || !pmd_present(*pmd))
		return (pte_t *)pmd;

	*level = PG_LEVEL_4K;

	return pte_offset_kernel(pmd, address);
}
EXPORT_SYMBOL_GPL(lookup_address);

/*
 * Set the new pmd in all the pgds we know about:
 */
static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
{
	/* change init_mm */
	set_pte_atomic(kpte, pte);
#ifdef CONFIG_X86_32
	if (!SHARED_KERNEL_PMD) {
		struct page *page;

		list_for_each_entry(page, &pgd_list, lru) {
			pgd_t *pgd;
			pud_t *pud;
			pmd_t *pmd;

			pgd = (pgd_t *)page_address(page) + pgd_index(address);
			pud = pud_offset(pgd, address);
			pmd = pmd_offset(pud, address);
			set_pte_atomic((pte_t *)pmd, pte);
		}
	}
#endif
}

static int
try_preserve_large_page(pte_t *kpte, unsigned long address,
			struct cpa_data *cpa)
{
	unsigned long nextpage_addr, numpages, pmask, psize, flags, addr, pfn;
	pte_t new_pte, old_pte, *tmp;
	pgprot_t old_prot, new_prot;
	int i, do_split = 1;
	unsigned int level;

	if (cpa->force_split)
		return 1;

	spin_lock_irqsave(&pgd_lock, flags);
	/*
	 * Check for races, another CPU might have split this page
	 * up already:
	 */
	tmp = lookup_address(address, &level);
	if (tmp != kpte)
		goto out_unlock;

	switch (level) {
	case PG_LEVEL_2M:
		psize = PMD_PAGE_SIZE;
		pmask = PMD_PAGE_MASK;
		break;
#ifdef CONFIG_X86_64
	case PG_LEVEL_1G:
		psize = PUD_PAGE_SIZE;
		pmask = PUD_PAGE_MASK;
		break;
#endif
	default:
		do_split = -EINVAL;
		goto out_unlock;
	}

	/*
	 * Calculate the number of pages, which fit into this large
	 * page starting at address:
	 */
	nextpage_addr = (address + psize) & pmask;
	numpages = (nextpage_addr - address) >> PAGE_SHIFT;
	if (numpages < cpa->numpages)
		cpa->numpages = numpages;

	/*
	 * We are safe now. Check whether the new pgprot is the same:
	 */
	old_pte = *kpte;
	old_prot = new_prot = pte_pgprot(old_pte);

	pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
	pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);

	/*
	 * old_pte points to the large page base address. So we need
	 * to add the offset of the virtual address:
	 */
	pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
	cpa->pfn = pfn;

	new_prot = static_protections(new_prot, address, pfn);

	/*
	 * We need to check the full range, whether
	 * static_protection() requires a different pgprot for one of
	 * the pages in the range we try to preserve:
	 */
	addr = address + PAGE_SIZE;
	pfn++;
	for (i = 1; i < cpa->numpages; i++, addr += PAGE_SIZE, pfn++) {
		pgprot_t chk_prot = static_protections(new_prot, addr, pfn);

		if (pgprot_val(chk_prot) != pgprot_val(new_prot))
			goto out_unlock;
	}

	/*
	 * If there are no changes, return. maxpages has been updated
	 * above:
	 */
	if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
		do_split = 0;
		goto out_unlock;
	}

	/*
	 * We need to change the attributes. Check, whether we can
	 * change the large page in one go. We request a split, when
	 * the address is not aligned and the number of pages is
	 * smaller than the number of pages in the large page. Note
	 * that we limited the number of possible pages already to
	 * the number of pages in the large page.
	 */
	if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
		/*
		 * The address is aligned and the number of pages
		 * covers the full page.
		 */
		new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
		__set_pmd_pte(kpte, address, new_pte);
		cpa->flags |= CPA_FLUSHTLB;
		do_split = 0;
	}

out_unlock:
	spin_unlock_irqrestore(&pgd_lock, flags);

	return do_split;
}

static int split_large_page(pte_t *kpte, unsigned long address)
{
	unsigned long flags, pfn, pfninc = 1;
	unsigned int i, level;
	pte_t *pbase, *tmp;
	pgprot_t ref_prot;
	struct page *base;

	if (!debug_pagealloc)
		spin_unlock(&cpa_lock);
	base = alloc_pages(GFP_KERNEL, 0);
	if (!debug_pagealloc)
		spin_lock(&cpa_lock);
	if (!base)
		return -ENOMEM;