eal/windows: implement basic memory management
[dpdk.git] / lib / librte_eal / windows / eal.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4
5 #include <fcntl.h>
6 #include <io.h>
7 #include <share.h>
8 #include <sys/stat.h>
9
10 #include <rte_debug.h>
11 #include <rte_eal.h>
12 #include <eal_memcfg.h>
13 #include <rte_errno.h>
14 #include <rte_lcore.h>
15 #include <eal_thread.h>
16 #include <eal_internal_cfg.h>
17 #include <eal_filesystem.h>
18 #include <eal_options.h>
19 #include <eal_private.h>
20 #include <rte_trace_point.h>
21
22 #include "eal_hugepages.h"
23 #include "eal_windows.h"
24
25 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
26
27  /* Allow the application to print its usage message too if set */
28 static rte_usage_hook_t rte_application_usage_hook;
29
30 /* define fd variable here, because file needs to be kept open for the
31  * duration of the program, as we hold a write lock on it in the primary proc
32  */
33 static int mem_cfg_fd = -1;
34
35 /* early configuration structure, when memory config is not mmapped */
36 static struct rte_mem_config early_mem_config;
37
38 /* Address of global and public configuration */
39 static struct rte_config rte_config = {
40                 .mem_config = &early_mem_config,
41 };
42
43 /* internal configuration (per-core) */
44 struct lcore_config lcore_config[RTE_MAX_LCORE];
45
46 /* internal configuration */
47 struct internal_config internal_config;
48
49 /* platform-specific runtime dir */
50 static char runtime_dir[PATH_MAX];
51
52 const char *
53 rte_eal_get_runtime_dir(void)
54 {
55         return runtime_dir;
56 }
57
58 /* Return a pointer to the configuration structure */
59 struct rte_config *
60 rte_eal_get_configuration(void)
61 {
62         return &rte_config;
63 }
64
65 /* Detect if we are a primary or a secondary process */
66 enum rte_proc_type_t
67 eal_proc_type_detect(void)
68 {
69         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
70         const char *pathname = eal_runtime_config_path();
71
72         /* if we can open the file but not get a write-lock we are a secondary
73          * process. NOTE: if we get a file handle back, we keep that open
74          * and don't close it to prevent a race condition between multiple opens
75          */
76         errno_t err = _sopen_s(&mem_cfg_fd, pathname,
77                 _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE);
78         if (err == 0) {
79                 OVERLAPPED soverlapped = { 0 };
80                 soverlapped.Offset = sizeof(*rte_config.mem_config);
81                 soverlapped.OffsetHigh = 0;
82
83                 HANDLE hwinfilehandle = (HANDLE)_get_osfhandle(mem_cfg_fd);
84
85                 if (!LockFileEx(hwinfilehandle,
86                         LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0,
87                         sizeof(*rte_config.mem_config), 0, &soverlapped))
88                         ptype = RTE_PROC_SECONDARY;
89         }
90
91         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
92                 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
93
94         return ptype;
95 }
96
97 enum rte_proc_type_t
98 rte_eal_process_type(void)
99 {
100         return rte_config.process_type;
101 }
102
103 int
104 rte_eal_has_hugepages(void)
105 {
106         return !internal_config.no_hugetlbfs;
107 }
108
109 enum rte_iova_mode
110 rte_eal_iova_mode(void)
111 {
112         return rte_config.iova_mode;
113 }
114
115 /* display usage */
116 static void
117 eal_usage(const char *prgname)
118 {
119         printf("\nUsage: %s ", prgname);
120         eal_common_usage();
121         /* Allow the application to print its usage message too
122          * if hook is set
123          */
124         if (rte_application_usage_hook) {
125                 printf("===== Application Usage =====\n\n");
126                 rte_application_usage_hook(prgname);
127         }
128 }
129
130 /* Parse the arguments for --log-level only */
131 static void
132 eal_log_level_parse(int argc, char **argv)
133 {
134         int opt;
135         char **argvopt;
136         int option_index;
137
138         argvopt = argv;
139
140         eal_reset_internal_config(&internal_config);
141
142         while ((opt = getopt_long(argc, argvopt, eal_short_options,
143                 eal_long_options, &option_index)) != EOF) {
144
145                 int ret;
146
147                 /* getopt is not happy, stop right now */
148                 if (opt == '?')
149                         break;
150
151                 ret = (opt == OPT_LOG_LEVEL_NUM) ?
152                         eal_parse_common_option(opt, optarg,
153                                 &internal_config) : 0;
154
155                 /* common parser is not happy */
156                 if (ret < 0)
157                         break;
158         }
159
160         optind = 0; /* reset getopt lib */
161 }
162
163 /* Parse the argument given in the command line of the application */
164 static int
165 eal_parse_args(int argc, char **argv)
166 {
167         int opt, ret;
168         char **argvopt;
169         int option_index;
170         char *prgname = argv[0];
171
172         argvopt = argv;
173
174         while ((opt = getopt_long(argc, argvopt, eal_short_options,
175                 eal_long_options, &option_index)) != EOF) {
176
177                 int ret;
178
179                 /* getopt is not happy, stop right now */
180                 if (opt == '?') {
181                         eal_usage(prgname);
182                         return -1;
183                 }
184
185                 ret = eal_parse_common_option(opt, optarg, &internal_config);
186                 /* common parser is not happy */
187                 if (ret < 0) {
188                         eal_usage(prgname);
189                         return -1;
190                 }
191                 /* common parser handled this option */
192                 if (ret == 0)
193                         continue;
194
195                 switch (opt) {
196                 case 'h':
197                         eal_usage(prgname);
198                         exit(EXIT_SUCCESS);
199                 default:
200                         if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
201                                 RTE_LOG(ERR, EAL, "Option %c is not supported "
202                                         "on Windows\n", opt);
203                         } else if (opt >= OPT_LONG_MIN_NUM &&
204                                 opt < OPT_LONG_MAX_NUM) {
205                                 RTE_LOG(ERR, EAL, "Option %s is not supported "
206                                         "on Windows\n",
207                                         eal_long_options[option_index].name);
208                         } else {
209                                 RTE_LOG(ERR, EAL, "Option %d is not supported "
210                                         "on Windows\n", opt);
211                         }
212                         eal_usage(prgname);
213                         return -1;
214                 }
215         }
216
217         if (eal_adjust_config(&internal_config) != 0)
218                 return -1;
219
220         /* sanity checks */
221         if (eal_check_common_options(&internal_config) != 0) {
222                 eal_usage(prgname);
223                 return -1;
224         }
225
226         if (optind >= 0)
227                 argv[optind - 1] = prgname;
228         ret = optind - 1;
229         optind = 0; /* reset getopt lib */
230         return ret;
231 }
232
233 static int
234 sync_func(void *arg __rte_unused)
235 {
236         return 0;
237 }
238
239 static void
240 rte_eal_init_alert(const char *msg)
241 {
242         fprintf(stderr, "EAL: FATAL: %s\n", msg);
243         RTE_LOG(ERR, EAL, "%s\n", msg);
244 }
245
246 /* Stubs to enable EAL trace point compilation
247  * until eal_common_trace.c can be compiled.
248  */
249
250 RTE_DEFINE_PER_LCORE(volatile int, trace_point_sz);
251 RTE_DEFINE_PER_LCORE(void *, trace_mem);
252
253 void
254 __rte_trace_mem_per_thread_alloc(void)
255 {
256 }
257
258 void
259 __rte_trace_point_emit_field(size_t sz, const char *field,
260         const char *type)
261 {
262         RTE_SET_USED(sz);
263         RTE_SET_USED(field);
264         RTE_SET_USED(type);
265 }
266
267 int
268 __rte_trace_point_register(rte_trace_point_t *trace, const char *name,
269         void (*register_fn)(void))
270 {
271         RTE_SET_USED(trace);
272         RTE_SET_USED(name);
273         RTE_SET_USED(register_fn);
274         return -ENOTSUP;
275 }
276
277  /* Launch threads, called at application init(). */
278 int
279 rte_eal_init(int argc, char **argv)
280 {
281         int i, fctret;
282
283         rte_eal_log_init(NULL, 0);
284
285         eal_log_level_parse(argc, argv);
286
287         if (eal_create_cpu_map() < 0) {
288                 rte_eal_init_alert("Cannot discover CPU and NUMA.");
289                 /* rte_errno is set */
290                 return -1;
291         }
292
293         if (rte_eal_cpu_init() < 0) {
294                 rte_eal_init_alert("Cannot detect lcores.");
295                 rte_errno = ENOTSUP;
296                 return -1;
297         }
298
299         fctret = eal_parse_args(argc, argv);
300         if (fctret < 0)
301                 exit(1);
302
303         /* Prevent creation of shared memory files. */
304         if (internal_config.in_memory == 0) {
305                 RTE_LOG(WARNING, EAL, "Multi-process support is requested, "
306                         "but not available.\n");
307                 internal_config.in_memory = 1;
308         }
309
310         if (!internal_config.no_hugetlbfs && (eal_hugepage_info_init() < 0)) {
311                 rte_eal_init_alert("Cannot get hugepage information");
312                 rte_errno = EACCES;
313                 return -1;
314         }
315
316         if (internal_config.memory == 0 && !internal_config.force_sockets) {
317                 if (internal_config.no_hugetlbfs)
318                         internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
319         }
320
321         if (eal_mem_win32api_init() < 0) {
322                 rte_eal_init_alert("Cannot access Win32 memory management");
323                 rte_errno = ENOTSUP;
324                 return -1;
325         }
326
327         if (eal_mem_virt2iova_init() < 0) {
328                 /* Non-fatal error if physical addresses are not required. */
329                 RTE_LOG(WARNING, EAL, "Cannot access virt2phys driver, "
330                         "PA will not be available\n");
331         }
332
333         if (rte_eal_memzone_init() < 0) {
334                 rte_eal_init_alert("Cannot init memzone");
335                 rte_errno = ENODEV;
336                 return -1;
337         }
338
339         if (rte_eal_memory_init() < 0) {
340                 rte_eal_init_alert("Cannot init memory");
341                 rte_errno = ENOMEM;
342                 return -1;
343         }
344
345         if (rte_eal_malloc_heap_init() < 0) {
346                 rte_eal_init_alert("Cannot init malloc heap");
347                 rte_errno = ENODEV;
348                 return -1;
349         }
350
351         if (rte_eal_tailqs_init() < 0) {
352                 rte_eal_init_alert("Cannot init tail queues for objects");
353                 rte_errno = EFAULT;
354                 return -1;
355         }
356
357         eal_thread_init_master(rte_config.master_lcore);
358
359         RTE_LCORE_FOREACH_SLAVE(i) {
360
361                 /*
362                  * create communication pipes between master thread
363                  * and children
364                  */
365                 if (_pipe(lcore_config[i].pipe_master2slave,
366                         sizeof(char), _O_BINARY) < 0)
367                         rte_panic("Cannot create pipe\n");
368                 if (_pipe(lcore_config[i].pipe_slave2master,
369                         sizeof(char), _O_BINARY) < 0)
370                         rte_panic("Cannot create pipe\n");
371
372                 lcore_config[i].state = WAIT;
373
374                 /* create a thread for each lcore */
375                 if (eal_thread_create(&lcore_config[i].thread_id) != 0)
376                         rte_panic("Cannot create thread\n");
377         }
378
379         /*
380          * Launch a dummy function on all slave lcores, so that master lcore
381          * knows they are all ready when this function returns.
382          */
383         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
384         rte_eal_mp_wait_lcore();
385         return fctret;
386 }