linux.cc revision 9143
17139Sgblack@eecs.umich.edu/*
27139Sgblack@eecs.umich.edu * Copyright (c) 2009 The Regents of The University of Michigan
37139Sgblack@eecs.umich.edu * All rights reserved.
47139Sgblack@eecs.umich.edu *
57139Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
67139Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
77139Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
87139Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
97139Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
107139Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
117139Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
127139Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
137139Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
147139Sgblack@eecs.umich.edu * this software without specific prior written permission.
157139Sgblack@eecs.umich.edu *
167139Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177139Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187139Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
197139Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
207139Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
217139Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
227139Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237139Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247139Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257139Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
267139Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277139Sgblack@eecs.umich.edu *
287139Sgblack@eecs.umich.edu * Authors: Ali Saidi
297139Sgblack@eecs.umich.edu */
307139Sgblack@eecs.umich.edu
317139Sgblack@eecs.umich.edu#include <cstdio>
327139Sgblack@eecs.umich.edu#include <string>
337139Sgblack@eecs.umich.edu
347139Sgblack@eecs.umich.edu#include "cpu/thread_context.hh"
357139Sgblack@eecs.umich.edu#include "debug/SyscallVerbose.hh"
367139Sgblack@eecs.umich.edu#include "kern/linux/linux.hh"
377139Sgblack@eecs.umich.edu#include "sim/process.hh"
387139Sgblack@eecs.umich.edu#include "sim/system.hh"
397139Sgblack@eecs.umich.edu
407139Sgblack@eecs.umich.eduint
417139Sgblack@eecs.umich.eduLinux::openSpecialFile(std::string path, LiveProcess *process,
427139Sgblack@eecs.umich.edu                       ThreadContext *tc)
437146Sgblack@eecs.umich.edu{
447141Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, "Opening special file: %s\n", path.c_str());
457139Sgblack@eecs.umich.edu    if (path.compare(0, 13, "/proc/meminfo") == 0) {
467146Sgblack@eecs.umich.edu        std::string data = Linux::procMeminfo(process, tc);
477141Sgblack@eecs.umich.edu        FILE *f = tmpfile();
487139Sgblack@eecs.umich.edu        int fd = fileno(f);
497139Sgblack@eecs.umich.edu        size_t ret M5_VAR_USED = fwrite(data.c_str(), 1, data.size(), f);
507139Sgblack@eecs.umich.edu        assert(ret == data.size());
517146Sgblack@eecs.umich.edu        rewind(f);
527141Sgblack@eecs.umich.edu        return fd;
537139Sgblack@eecs.umich.edu    }
547146Sgblack@eecs.umich.edu
557141Sgblack@eecs.umich.edu    warn("Attempting to open special file: %s. Ignoring. Simulation may"
567139Sgblack@eecs.umich.edu            " take un-expected code path or be non-deterministic until proper"
577139Sgblack@eecs.umich.edu            "  handling is implemented.\n", path.c_str());
587139Sgblack@eecs.umich.edu    return -1;
597139Sgblack@eecs.umich.edu}
607139Sgblack@eecs.umich.edu
617141Sgblack@eecs.umich.edustd::string
627139Sgblack@eecs.umich.eduLinux::procMeminfo(LiveProcess *process, ThreadContext *tc)
637139Sgblack@eecs.umich.edu{
647141Sgblack@eecs.umich.edu    return csprintf("MemTotal:%12d kB\nMemFree: %12d kB\n",
657141Sgblack@eecs.umich.edu            process->system->memSize() >> 10,
667141Sgblack@eecs.umich.edu            process->system->freeMemSize() >> 10);
677139Sgblack@eecs.umich.edu}
687139Sgblack@eecs.umich.edu
697139Sgblack@eecs.umich.edu