-
Notifications
You must be signed in to change notification settings - Fork 1
/
mprotect.h
197 lines (160 loc) · 5.35 KB
/
mprotect.h
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
/*
* BRIEF DESCRIPTION
*
* Memory protection definitions for the NOVA filesystem.
*
* Copyright 2015-2016 Regents of the University of California,
* UCSD Non-Volatile Systems Lab, Andiry Xu <jix024@cs.ucsd.edu>
* Copyright 2012-2013 Intel Corporation
* Copyright 2009-2011 Marco Stornelli <marco.stornelli@gmail.com>
* Copyright 2003 Sony Corporation
* Copyright 2003 Matsushita Electric Industrial Co., Ltd.
* 2003-2004 (c) MontaVista Software, Inc. , Steve Longerbeam
*
* This program is free software; you can redistribute it and/or modify it
*
* This file is licensed under the terms of the GNU General Public
* License version 2. This program is licensed "as is" without any
* warranty of any kind, whether express or implied.
*/
#ifndef __WPROTECT_H
#define __WPROTECT_H
#include <linux/fs.h>
#include "nova_def.h"
#include "super.h"
extern void nova_error_mng(struct super_block *sb, const char *fmt, ...);
static inline int nova_range_check(struct super_block *sb, void *p,
unsigned long len)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
unsigned long first_virt_end = (unsigned long) sbi->virt_addr + (unsigned long) sbi->initsize;
unsigned long second_virt_end = (unsigned long) sbi->virt_addr_2 + (unsigned long) sbi->initsize_2;
unsigned long first_virt_start = (unsigned long) sbi->virt_addr;
unsigned long second_virt_start = (unsigned long) sbi->virt_addr_2;
if ((unsigned long) p < first_virt_start ||
((unsigned long) p >= first_virt_end && (unsigned long) p < second_virt_start) ||
((unsigned long) p + len >= first_virt_end && (unsigned long) p + len < second_virt_start) ||
(unsigned long) p + len >= second_virt_end) {
nova_err(sb, "access pmem out of range: pmem range 0x%lx - 0x%lx, "
"access range 0x%lx - 0x%lx\n",
(unsigned long)sbi->virt_addr,
(unsigned long)(sbi->virt_addr_2 + sbi->initsize_2),
(unsigned long)p, (unsigned long)(p + len));
dump_stack();
return -EINVAL;
}
return 0;
}
extern int nova_writeable(void *vaddr, unsigned long size, int rw);
static inline int nova_is_protected(struct super_block *sb)
{
struct nova_sb_info *sbi = (struct nova_sb_info *)sb->s_fs_info;
if (wprotect)
return wprotect;
return sbi->s_mount_opt & NOVA_MOUNT_PROTECT;
}
static inline int nova_is_wprotected(struct super_block *sb)
{
return nova_is_protected(sb);
}
static inline void
__nova_memunlock_range(void *p, unsigned long len)
{
/*
* NOTE: Ideally we should lock all the kernel to be memory safe
* and avoid to write in the protected memory,
* obviously it's not possible, so we only serialize
* the operations at fs level. We can't disable the interrupts
* because we could have a deadlock in this path.
*/
nova_writeable(p, len, 1);
}
static inline void
__nova_memlock_range(void *p, unsigned long len)
{
nova_writeable(p, len, 0);
}
static inline void nova_memunlock_range(struct super_block *sb, void *p,
unsigned long len)
{
if (nova_range_check(sb, p, len))
return;
if (nova_is_protected(sb))
__nova_memunlock_range(p, len);
}
static inline void nova_memlock_range(struct super_block *sb, void *p,
unsigned long len)
{
if (nova_is_protected(sb))
__nova_memlock_range(p, len);
}
static inline void nova_memunlock_super(struct super_block *sb)
{
struct nova_super_block *ps = nova_get_super(sb);
if (nova_is_protected(sb))
__nova_memunlock_range(ps, NOVA_SB_SIZE);
}
static inline void nova_memlock_super(struct super_block *sb)
{
struct nova_super_block *ps = nova_get_super(sb);
if (nova_is_protected(sb))
__nova_memlock_range(ps, NOVA_SB_SIZE);
}
static inline void nova_memunlock_reserved(struct super_block *sb,
struct nova_super_block *ps)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
if (nova_is_protected(sb))
__nova_memunlock_range(ps,
sbi->head_reserved_blocks * NOVA_DEF_BLOCK_SIZE_4K);
}
static inline void nova_memlock_reserved(struct super_block *sb,
struct nova_super_block *ps)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
if (nova_is_protected(sb))
__nova_memlock_range(ps,
sbi->head_reserved_blocks * NOVA_DEF_BLOCK_SIZE_4K);
}
static inline void nova_memunlock_journal(struct super_block *sb)
{
void *addr = nova_get_block(sb, NOVA_DEF_BLOCK_SIZE_4K * JOURNAL_START);
if (nova_range_check(sb, addr, NOVA_DEF_BLOCK_SIZE_4K))
return;
if (nova_is_protected(sb))
__nova_memunlock_range(addr, NOVA_DEF_BLOCK_SIZE_4K);
}
static inline void nova_memlock_journal(struct super_block *sb)
{
void *addr = nova_get_block(sb, NOVA_DEF_BLOCK_SIZE_4K * JOURNAL_START);
if (nova_is_protected(sb))
__nova_memlock_range(addr, NOVA_DEF_BLOCK_SIZE_4K);
}
static inline void nova_memunlock_inode(struct super_block *sb,
struct nova_inode *pi)
{
if (nova_range_check(sb, pi, NOVA_INODE_SIZE))
return;
if (nova_is_protected(sb))
__nova_memunlock_range(pi, NOVA_INODE_SIZE);
}
static inline void nova_memlock_inode(struct super_block *sb,
struct nova_inode *pi)
{
/* nova_sync_inode(pi); */
if (nova_is_protected(sb))
__nova_memlock_range(pi, NOVA_INODE_SIZE);
}
static inline void nova_memunlock_block(struct super_block *sb, void *bp)
{
if (nova_range_check(sb, bp, sb->s_blocksize))
return;
if (nova_is_protected(sb))
__nova_memunlock_range(bp, sb->s_blocksize);
}
static inline void nova_memlock_block(struct super_block *sb, void *bp)
{
if (nova_is_protected(sb))
__nova_memlock_range(bp, sb->s_blocksize);
}
#endif